我误解reloadRowsAtIndexPaths:

时间:2009-11-12 13:33:31

标签: iphone iphone-sdk-3.0

我有一个分组表视图,我想重新加载一个特定的行。当我调用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

4 个答案:

答案 0 :(得分:10)

尝试在

中包装您的ReloadRowsAtIndexPaths
[self.tableView beginUpdates];
[self.tableView endUpdates];

e.g

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

答案 1 :(得分:5)

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

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
reuseIdentifier:@"workoutTimerCell"] autorelease];

答案 2 :(得分:3)

我遇到了同样的问题,显然将withRowAnimation:设置为UITableViewRowAnimationNone为我解决了这个问题。 不能说为什么,但是否则细胞制作了一个很好的动画,然后就消失了。

答案 3 :(得分:3)

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