iOS

UITableView中的一些注意点

2015-06-22  本文已影响1746人  coderJerry

性能优化

/**
 *  什么时候调用:每当有一个cell进入视野范围内就会调用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";

    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        //NSLog(@"%p--------", cell);
    }

    // 3.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    NSLog(@"%p--------", cell);

    return cell;
}

重用标识的使用dequeue--出列,离队的意思

// 1.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        //NSLog(@"%p--------", cell);
    }
- 方法2:在ViewDidLoad中注册
// 2.注册某个标识对应的cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
- 方法3:在storyboard中右边框中填写identifier

这三种方法都可以让释放池中创建出一个想要的重用标识,选择其一即可。

UITableViewDelegate中的一些常用方法

//点击某个cell的时候调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
}
//取消选中某一行的时候调用
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
typedef enum {
   UITableViewStylePlain,//没有分section组
   UITableViewStyleGrouped//有分section组
} UITableViewStyle;

例如:
创建tableView时定义样式:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

不仅仅可以返回文字标题,还可以返回图片

//告诉tableView第section显示怎样的头部控件
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
//告诉tableView第section显示怎样的脚部控件
- (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section

设置分隔线

self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorEffect = ...;
self.tableView.separatorInset = ...;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
...(自己可以查看API)

设置选中样式

typedef enum : NSInteger {
   UITableViewCellSelectionStyleNone,//没有样式
   UITableViewCellSelectionStyleBlue,//蓝色
   UITableViewCellSelectionStyleGray,//灰色
   UITableViewCellSelectionStyleDefault//默认样式
} UITableViewCellSelectionStyle;
//设置为没有样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;

不过要注意的是,虽然是没有样式的,但是点击还是会有作用的。(即如果有didSelectRowAtIndexPath:,点击了还是会调用的)

设置选中的背景颜色

//设置选中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
//设置默认的背景色
cell.backgroundColor = [UIColor blueColor];
//设置默认的背景色,这个背景色的优先级高于backgroundColor
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView = backgroundView;

设置指示器

// 设置指示器
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//小箭头
cell.accessoryType = UITableViewCellAccessoryCheckmark;//打钩
cell.accessoryType = UITableViewCellAccessoryDetailButton;//感叹号提示
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//感叹号+小箭头
cell.accessoryType = UITableViewCellAccessoryNone;//啥都没有

同样的accessory指示器也可以添加其他的控件

//添加一个开关指示器
cell.accessoryView = [[UISwitch alloc] init];

KVC

KVC-Key Value Coding(键值编码)

//KVC,就是可以自动转为deal.title = dict[@"title"];
[deal setValuesForKeysWithDictionary:dict];

tag的用法

tag虽然好用,但请不要过度依赖哦~
因为当项目大了以后,我们设置的tag如果很多的话,那tag的编号会很混乱,每次要用到了就要找tag的编号,这样的效率是很低的。所以能不用就不用。

//设置代理,并调用代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //设置重用标识
    static NSString *ID = @"deal";
    //记得把storyboard或xib右边的identifier填入deal
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 取出模型数据
    XMGDeal *deal = self.deals[indexPath.row];

    // 设置数据
    UIImageView *iconView = (UIImageView *)[cell viewWithTag:10];
    iconView.image = [UIImage imageNamed:deal.icon];

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:20];
    titleLabel.text = deal.title;

    UILabel *priceLabel = (UILabel *)[cell viewWithTag:30];
    priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];

    UILabel *buyCountLabel = (UILabel *)[cell viewWithTag:40];
    buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
    return cell;
}

估计高度的作用

 #pragma mark - <UITableViewDataSource>
// tableView的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
    return self.messages.count;
 }
// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    XMGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"message”];
    cell.message = self.messages[indexPath.row];
    return cell;
 }

 #pragma mark - <UITableViewDelegate>

// 设置估计高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    return 200;
 }
// 创建出cell后再计算出具体的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    XMGMessage *message = self.messages[indexPath.row];
    return message.cellHeight;
 }

获取最大布局的宽度

这个属性会在布局约束时影响label的宽度的最大值,如果一个文字超出了屏幕的最大实际宽度,那么这个文字就会流动到下一行,这样就增加了label的高度。
利用了这个方法以后,我们可以更加精确的得到label的实际高度,以便我们更准确的布局。所以这个方法一般用于高度计算出现小偏差的时候。

self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;

注:文中若有错误,请及时和我交流,感激不尽~~

上一篇 下一篇

猜你喜欢

热点阅读