美文网首页其他
ios UItableViewCell detailTextLa

ios UItableViewCell detailTextLa

作者: 景彧 | 来源:发表于2016-11-03 13:09 被阅读3092次

tableView这个控件是很常用的,使用到它就会一定遇到UITableViewCell,然而接踵而至的cell重用、tableView优化什么的问题就来了。😂😂😂

一、问题:

下面的这个例子是为了解决Cell重用而出现让我苦恼的问题:

static NSString *cellIdentifier = @"NotesCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: cellIdentifier];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier
                                                            forIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *noteDictionary = [self.notes objectAtIndex: indexPath.row];
    cell.textLabel.text = noteDictionary[@"title"];
    cell.detailTextLabel.text = [self.dates objectAtIndex: indexPath.row];
    
    return cell;
}

按照上面的写法,确实是让cell重用了。但是问题来了,cell的detailTextLabel一直显示是空的,什么也看不见。
原因在于:在控制器的- (void)viewDidLoad;方法中注册cell的方法

- (void)  registerClass:(nullable Class)cellClass 
 forCellReuseIdentifier:(NSString *)identifier

和在

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{

}

方法中使用cell重用的方法

- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier 
                                          forIndexPath:(NSIndexPath *)indexPath;

是冲突的。

因为在下面的这个方法中所重用的cell都是在控制器- (void)viewDidLoad;方法中注册的cell,cell的样式都是默认的UITableViewCellStyleDefault样式,所以无论在下面的这个方法中对cell无论进行什么样的样式设置都是没有用的,重用到的cell的样式都是预先注册好的cell的UITableViewCellStyleDefault样式。

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{

} 

二、解决办法

无需在控制器的- (void)viewDidLoad;方法注册cell,只需要在下面的这个方法中这样实现就行了,通过tableView中indexPath的使用就可以实现重用,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *noteDictionary = [self.notes objectAtIndex: indexPath.row];
    cell.textLabel.text = noteDictionary[@"title"];
    cell.detailTextLabel.text = [self.dates objectAtIndex: indexPath.row];
    
    return cell;
}

关键是使用了下面的这个方法进行cell的重用:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

相关文章

网友评论

  • Auditore:cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
    reuseIdentifier: cellIdentifier];
    关键是这个UITableViewCellStyleValue1 如果设置成别的类型 就没有detailLabel了, 所以一定要用这个类型
  • 自负的大撸sir:手动赞
    景彧:@自负的大撸sir 有心
  • 吖几角:点个赞

本文标题:ios UItableViewCell detailTextLa

本文链接:https://www.haomeiwen.com/subject/igqbuttx.html