reloadRowsAtIndexPaths无法按预期重新加载索引路径

时间:2017-01-07 07:11:08

标签: ios objective-c uitableview

我在indexPath [0,0]添加图像作为头像,并在indexPath [0,1]上使用detailLabel显示昵称。  当使用reloadRowsAtIndexPaths indexPath时,indexPath [0,1]得到一个图像......

我在调用reloadRowsAtIndexPaths时发现出队单元格返回nil。

也许[0,0]单元格在[0,1]重用,我不知道为什么会这样。

enter image description here

代码是:

- (void)viewDidLoad {
    [super viewDidLoad];
    _titles = @[@"Avator", @"Nickname"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        _avator = [UIImage imageNamed:@"cat"];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _nickName = @"Smallfly";
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        });
    });
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *const cellIdenfier = @"cellIdentifier";

    // Why reloadRowsAtIndexPaths [0,0] returned cell is nil?
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdenfier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdenfier];
    }

    if (indexPath.row == 0) {
        [self configureCell:cell indexPath:indexPath];
    } else {
        NSString *nickName =  _nickName ?: @"nickname";
        cell.detailTextLabel.text = nickName;
    }
    cell.textLabel.text = _titles[indexPath.row];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {

    for (UIView *sv in cell.contentView.subviews) {
        if ([sv isKindOfClass:[UIImageView class]]) {
            [sv removeFromSuperview];
        }
    }

    UIImage *avator = _avator ?: [UIImage imageNamed:@"user_profile_avatar"];
    UIImageView *avatorImageView = [[UIImageView alloc] initWithImage:avator];
    ...
    [cell.contentView addSubview:avatorImageView];
}

1 个答案:

答案 0 :(得分:0)

  • reloadRowsAtIndexPaths是一种void方法。

  • dequeueReusableCell始终返回一个单元格(删除UITableViewCell alloc

目标C

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
      withRowAnimation:(UITableViewRowAnimation)animation

- (UITableViewCell *)tableView:(UITableView *)tableView
                               cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = 
        [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"
        forIndexPath:indexPath];
    // Configure the cell...
    return cell;
}

Swift 3

open func reloadRows(at indexPaths: [IndexPath],
    with animation: UITableViewRowAnimation)

override func tableView(_ tableView: UITableView,
                        cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = 
        tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier",
        for: indexPath)
    // Configure the cell...
    return cell
}

您可能希望查看更新的教程或示例代码。