UI笔记iOS-swift

UI高级控件入门2——UITableView

2015-03-23  本文已影响607人  冷漠叻荭颜

UITableView

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
    UITableViewCellStyleValue1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
    UITableViewCellStyleValue2,        // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
    UITableViewCellStyleSubtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 设置tableView的组数 可以不实现 默认是1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 设置tableView的行数 必须实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 设置tableView每一行的cell 必须实现
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // 组头标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // 组尾标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // tableView右侧的索引
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; // 设置tableView的行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; // 设置tableView的组头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // 设置tableView的组尾的高度
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; // 选择了某组中的某行
@interface NSIndexPath (UITableView)
+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
@property(nonatomic,readonly) NSInteger section;
@property(nonatomic,readonly) NSInteger row;
@end
static NSString *reuseID = @"hero";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseID];
    // 不能在此设置cell的属性
}
NSLog(@"===%d", indexPath.row);
NSLog(@"%p", cell);

微博自定义cell示例

frame模型

#import <UIKit/UIKit.h>
#define KNAMEFONT [UIFont systemFontOfSize:15]
#define KTEXTFONT [UIFont systemFontOfSize:13]
@class LNMicroBlog;
@interface LNMicroBlogFrame : NSObject
// blogFrame模型
@property (nonatomic, strong) LNMicroBlog *microBlog;
@property (nonatomic, assign, readonly) CGRect iconF;
@property (nonatomic, assign, readonly) CGRect nameF;
@property (nonatomic, assign, readonly) CGRect vipF;
@property (nonatomic, assign, readonly) CGRect textF;
@property (nonatomic, assign, readonly) CGRect pictureF;
@property (nonatomic, assign, readonly) CGFloat rowHeight;
+ (NSArray *)microBlogFrames;
@end

#import "LNMicroBlogFrame.h"
#import "LNMicroBlog.h"
@implementation LNMicroBlogFrame
+ (NSArray *)microBlogFrames {
    NSArray *microBlogs = [LNMicroBlog microBlogs];
    NSMutableArray *tmpArray = [NSMutableArray array];
    for (LNMicroBlog *microBlog in microBlogs) {
        LNMicroBlogFrame *microBlogFrame = [[LNMicroBlogFrame alloc] init];
        microBlogFrame.microBlog = microBlog;
        [tmpArray addObject:microBlogFrame];
    }
    return tmpArray;
}
//  重写setter方法 
- (void)setMicroBlog:(LNMicroBlog *)microBlog {
    _microBlog = microBlog;
    [self setBlogFrame:microBlog]; // 给每个属性设置frame
}
// 依据数据模型,设置frame布局
- (void)setBlogFrame:(LNMicroBlog *)microBlog {
    CGFloat margin = 10;
    
    // 头像
    CGFloat iconW = 35;
    CGFloat iconH = iconW;
    CGFloat iconX = margin;
    CGFloat iconY = margin;
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 昵称
    CGFloat nameX = CGRectGetMaxX(_iconF) + margin;
    CGSize nameSize = [self sizeOfText:microBlog.name font:KNAMEFONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameY = margin + (iconH - nameSize.height) / 2;
    _nameF = (CGRect){{nameX, nameY}, nameSize};
    
    // vip
    CGFloat vipW = 14;
    CGFloat vipH = vipW;
    CGFloat vipX = CGRectGetMaxX(_nameF) + margin;
    CGFloat vipY = nameY;
    _vipF = CGRectMake(vipX, vipY, vipW, vipH);
    
    // 内容
    CGSize textSize = [self sizeOfText:microBlog.text font:KTEXTFONT maxSize:CGSizeMake(355, MAXFLOAT)];
    CGFloat textX = margin;
    CGFloat textY = CGRectGetMaxY(_iconF) + margin;
    _textF = (CGRect){{textX, textY}, textSize};
    
    // 图片
    if (microBlog.picture) {
        CGFloat pictureW = 200;
        CGFloat pictureH = pictureW;
        CGFloat pictureX = margin;
        CGFloat pictureY = CGRectGetMaxY(_textF) + margin;
        _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
        _rowHeight = margin + CGRectGetMaxY(_pictureF);
    } else {
        _rowHeight = margin + CGRectGetMaxY(_textF);
    }
}
// 封装计算多行文本数据size方法
- (CGSize)sizeOfText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {
   NSDictionary *attrs = @{NSFontAttributeName:font};
   CGSize size = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
   return size;
}
@end

自定义cell

#import <UIKit/UIKit.h>
@class LNMicroBlogFrame;
@interface LNMicroBlogCell : UITableViewCell
// 创建可重用的cell
+ (instancetype)microBlogWithTableView:(UITableView *)tableView;
// 拥有一个blog属性的frame
@property (nonatomic, strong) LNMicroBlogFrame *microBlogFrame;
@end

#import "LNMicroBlogCell.h"
#import "LNMicroBlogFrame.h"
#import "LNMicroBlog.h"
@interface LNMicroBlogCell ()
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *nameView;
@property (nonatomic, weak) UIImageView *vipView;
@property (nonatomic, weak) UILabel *textView;
@property (nonatomic, weak) UIImageView *pictureView;
@end

@implementation LNMicroBlogCell
// 快速创建cell的方法
+ (instancetype)microBlogWithTableView:(UITableView *)tableView {
    static NSString *reuseId = @"blogCache";
    LNMicroBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    return cell;
}
// 重写构造方法 设置cell内部的子控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        // 昵称
        UILabel *nameView = [[UILabel alloc] init];
        nameView.font = KNAMEFONT;
        [self.contentView addSubview:nameView];
        self.nameView = nameView;
        
        // vip
        UIImageView *vipView = [[UIImageView alloc] init];
        vipView.image = [UIImage imageNamed:@"vip"];
        [self.contentView addSubview:vipView];
        self.vipView = vipView;
        
        // 内容
        UILabel *textView = [[UILabel alloc] init];
        textView.font = KTEXTFONT;
        textView.numberOfLines = 0;
        [self.contentView addSubview:textView];
        self.textView = textView;
        
        // 图片
        UIImageView *pictureView = [[UIImageView alloc] init];
        [self.contentView addSubview:pictureView];
        self.pictureView = pictureView;
    }
    return self;
}
// 重写microBlogFrame的setter方法 给cell内部的子控件赋值 设置frame
- (void)setMicroBlogFrame:(LNMicroBlogFrame *)microBlogFrame {
    _microBlogFrame = microBlogFrame;
    // 给子控件赋值
    [self setSubviewsData:microBlogFrame];
    // 给子控件设置frame
    [self setSubviewsFrame:microBlogFrame];
}
// 给cell内部子控件赋值
- (void)setSubviewsData:(LNMicroBlogFrame *)microBlogFrame {
    LNMicroBlog *microBlog = microBlogFrame.microBlog;
    
    // 头像
    self.iconView.image = [UIImage imageNamed:microBlog.icon];
    
    // 姓名
    self.nameView.text = microBlog.name;
    
    // vip
    if (microBlog.vip) {
        self.vipView.hidden = NO;
        self.nameView.textColor = [UIColor redColor];
    } else {
        self.vipView.hidden = YES;
        self.nameView.textColor = [UIColor blackColor];
    }
    
    // 内容
    self.textView.text = microBlog.text;
    
    // 图片
    if (microBlog.picture) {
        self.pictureView.image = [UIImage imageNamed:microBlog.picture];
    } else {
        self.pictureView.image = nil;
    }
}
// 给子控件设置frame
- (void)setSubviewsFrame:(LNMicroBlogFrame *)microBlogFrame {
    self.iconView.frame = microBlogFrame.iconF;
    self.nameView.frame = microBlogFrame.nameF;
    self.vipView.frame = microBlogFrame.vipF;
    self.textView.frame = microBlogFrame.textF;
    self.pictureView.frame = microBlogFrame.pictureF;
}
- (CGSize)sizeWithAttributes:(NSDictionary *)attrs; // 计算单行文本数据文本宽度和高度
// 计算多行文本数据文本宽度和高度,在此之前应设置文本的numberOfLines属性为0
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context;
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; // 在指定indexPaths插入单\多行
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; // 在指定indexPaths删除单\多行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;  // 在指定indexPaths删除之前的行,然后加载刷新数据
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; // 让指定indexPath行滚动到屏幕指定的位置
上一篇下一篇

猜你喜欢

热点阅读