reloadRowsAtIndexPath与animateWithDuration混淆

时间:2015-11-13 09:40:06

标签: ios objective-c uitableview

我试图一次绑定两个动作。其中一个动态更改heighForRow第二个,动态拉伸View.frame。两者都是相同的点击。问题是当我尝试添加reloadRowsAtIndexPath时 触摸后,当行高度发生变化时,遗憾的是行标题和颜色会出现问题"。例如,触摸第2行的位置后,第1行显示,第3行显示第1行等,每次单击但是更改rowHeight工作!并且日志显示正确的行被触摸 - (所以当我触摸第一个时,第一个用NSLog记录)。有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil){

        cell = [[PrototypeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
                [cell setBackgroundColor:[UIColor clearColor]];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                [cell setIndentationWidth:0.0];

        PrototypeCell *prototypeCell =(PrototypeCell *)cell;
        prototypeCell.View = [[UIView alloc]init];
        prototypeCell.View.frame = CGRectMake(0,0, 15, 120);
        prototypeCell.View.alpha = .5f;
        [cell addSubview:prototypeCell.View];
        DetailView *detailView = [[DetailView alloc] initWithFrame:CGRectMake(0, 120, 320, 120)];
        [cell addSubview:detailView];
        prototypeCell.detailView = detailView;

        if (indexPath.row == 0){

                prototypeCell.View.backgroundColor = [UIColor redColor];
                cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

            }
        if (indexPath.row == 1){

                    prototypeCell.View.backgroundColor = [UIColor greenColor];
                    cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];


            }
        if (indexPath.row == 2){
                    prototypeCell.View.backgroundColor = [UIColor blueColor];
                    cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

            }}

        cell.textLabel.font = [UIFont boldSystemFontOfSize:21];
        cell.detailTextLabel.font = [UIFont italicSystemFontOfSize:13];
        cell.accessoryType = UITableViewCellAccessoryNone;

        PrototypeCell *prototypeCell =(PrototypeCell *)cell;
        prototypeCell.detailView.hidden = !expanded[indexPath.row];

        return cell;

}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    int selectedRow = indexPath.row;
    NSLog(@"touch on row %d ", selectedRow);

    PrototypeCell *prototypeCell =(PrototypeCell *)[tableView cellForRowAtIndexPath:indexPath];

    [self.tableView beginUpdates];
    expanded[indexPath.row] = !expanded[indexPath.row];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];

   if (prototypeCell.stretched == NO){
    [UIView animateWithDuration:1 animations:^{

        prototypeCell.View.frame = CGRectMake(0, 0, 320, 120);
    }
                     completion:nil];
                prototypeCell.stretched = YES;

    }

    else {

    [UIView animateWithDuration:1 animations:^{

            prototypeCell.View.frame = CGRectMake(0, 0, 15, 120);
    }
                     completion: nil];
            prototypeCell.stretched = NO;

    }
}

1 个答案:

答案 0 :(得分:1)

问题是如何使用dequeueReusableCellWithIdentifier,因为当重用单元格时,以下代码将无法执行。

if (indexPath.row == 0){

                prototypeCell.View.backgroundColor = [UIColor redColor];
                cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

            }
        if (indexPath.row == 1){

                    prototypeCell.View.backgroundColor = [UIColor greenColor];
                    cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];


            }
        if (indexPath.row == 2){
                    prototypeCell.View.backgroundColor = [UIColor blueColor];
                    cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

            }

此代码仅在您的单元格为零时才执行,如果您不使用重复单元格,则必须将此代码移出nil块,否则如果您的单元格有限且每个单元格都不同,则不需要。重用。

完成动画后,还要在最后移动这两行

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
相关问题