我是否误解了reloadRowsAtIndexPaths:方法?

9

我有一个分组的表视图,我想重新加载特定的行。当我调用reloadRowsAtIndexPaths时,该行从表视图中完全消失了。还需要做什么吗?

//this is the method doing the reload
-(void)setDurationTableViewCell:(NSString *)dur {

    self.workoutDuration = dur;
    NSIndexPath *durPath = [NSIndexPath indexPathForRow:3 inSection:0];
    NSArray *paths = [NSArray arrayWithObject:durPath];
    [woTableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationRight];

}//end setDurationTableViewCell


//this is the cellForRowAtIndexPath method if it has anything to do with my issue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;

    if (indexPath.row == 0) {

        //workout comments
        cell = [tableView dequeueReusableCellWithIdentifier:@"workoutCommentsCell"];
        if (nil == cell) { 
            cell = workoutCommentsCell;
            cell.selectionStyle = UITableViewCellStyleValue1;
        }

    }else if (indexPath.row == 1) {

        //difficulty
        cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDifficultyCell"];
        if (nil == cell) { 
            cell = workoutDifficultyCell;
            cell.selectionStyle = UITableViewCellStyleValue1;
        }
        cell.textLabel.text = [NSString stringWithFormat:@"Difficulty: %@", self.workoutDifficulty];

    }else if (indexPath.row == 2) {

        //workoutDate
        cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDateCell"];
        if (nil == cell) { 
            cell = workoutDateCell;
            cell.selectionStyle = UITableViewCellStyleValue1;
        }
        cell.textLabel.text = [NSString stringWithFormat:@"Date: %@", self.workoutDate];

    }else if (indexPath.row == 3) {

        //workoutDuration
        cell = [tableView dequeueReusableCellWithIdentifier:@"workoutTimerCell"];
        if (nil == cell) { 
            cell = workoutTimeCell;
            cell.selectionStyle = UITableViewCellStyleValue1;
        }
        cell.textLabel.text = [NSString stringWithFormat:@"Duration: %@", self.workoutDuration];

    }//end else-if


    return cell;

}//end cellForRowAtIndexPath

你解决这个问题了吗?我也遇到了相同的问题。 - MikeyWard
4个回答

10

尝试将 ReloadRowsAtIndexPaths 包裹在

[self.tableView beginUpdates];
[self.tableView endUpdates];

e.g

[self.tableView beginUpdates];
[woTableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates]

对我有帮助的是完全删除reloadRowsAtIndexPaths:,只留下 [self.tableView beginUpdates]; [self.tableView endUpdates]; - Krizai
仅供参考。我尝试将几个reloadRowsAtIndexPaths包装在一个beginUpdates endUpdates块中,但这导致了accessoryView的尴尬行为,它是一个存储在属性中的UISwitch。阅读文档时,begin/endUpdates用于插入、删除和选择。 - manuelwaldner

5

我最近开始学习iPhone SDK,很可能无法给你答案......但是,在if (nil == cell)块中,难道不需要替换代码为

if (cell == nil)
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
reuseIdentifier:@"workoutTimerCell"] autorelease];

1
+1 我同意 @CodeSeavers 的观点。看起来你没有分配任何单元格。即使你为 workoutDifficultyCell 分配了某些东西,你也需要为表格中的其他单元格分配更多的空间。我建议你在苹果的网站上查看 TableViewSuite 以了解更多信息。 - AWrightIV

4
我曾遇到同样的问题,后来发现将withRowAnimation:设置为UITableViewRowAnimationNone可以解决此问题。 不知道原因,但是这样做可以防止单元格在有动画效果后消失。

3
dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接