【问题标题】:UITableView Scroll automatically to the top after begin/end updates call for adjust heightUITableView 在开始/结束更新调用调整高度后自动滚动到顶部
【发布时间】:2014-07-24 17:39:10
【问题描述】:

我有一个 TableViewController 来初始化我的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FIDPostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell for this indexPath
    [cell updateFonts];
    [cell loadDataWithPost:[self.posts objectAtIndex:indexPath.item]];
    cell.parentTableViewController = self;
    cell.indexPath = indexPath;
    [cell draw];
    if(self.selectedIndex == indexPath.row){
        //Do expand cell stuff
    } else{
        //DO closed cell stuff
    }

    return cell;
}

并且响应 heightForRowAtIndexPath:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
  NSString *reuseIdentifier = CellIdentifier;
    FIDPostTableViewCell *cell = [self.offscreenCells objectForKey:reuseIdentifier];
    if (!cell) {
        cell = [[FIDPostTableViewCell alloc] init];
        [self.offscreenCells setObject:cell forKey:reuseIdentifier];
    }

    // Configure the cell for this indexPath
    [cell updateFonts];
    [cell loadDataForHeightCalculationWithPost:[self.posts objectAtIndex:indexPath.item]];
    [cell draw];

         if(self.selectedIndex == indexPath.row){
    return [cell calculateHeight] + 100;
} else{
    return [cell calculateHeight];
}


    }

self.selectedIndex是TableViewController的一个int局部变量

每个自定义单元格都有一个按钮,在触摸时响应选择器,这是我的 CustomViewCell 代码:

  self.expandSocialAction = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.expandSocialAction.backgroundColor = [FIDUIHelper fideniaLightBlue];
        [self.expandSocialAction addTarget:self action:@selector(selectRow:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.expandSocialAction];

然后:

-(void)selectRow:(id)sender{
    if(self.parentTableViewController.selectedIndex == self.indexPath.row){
        self.parentTableViewController.selectedIndex = -1;
    } else{
    self.parentTableViewController.selectedIndex = self.indexPath.row;
    }
    [self.parentTableViewController.tableView beginUpdates];
    [self.parentTableViewController.tableView endUpdates];

}

单元格有一个指向父 tableViewContorller 的指针:self.parentTableViewController。 一切正常,在 beginUpdate 和 endUpdates 调用之后,方法 heightForRowAtIndexPath 被调用(我在其中放置了一个断点)并且 che 单元格具有正确的高度。

如果我单击第一行或第二行上的按钮,单元格动画并改变高度很好,但如果我向下滚动表格,例如我点击第 6 行,高度会改变,但 tableView 会自动滚动到第一行或第二个元素。

有什么建议吗?

问候,

【问题讨论】:

    标签: ios xcode uitableview ios7


    【解决方案1】:

    我感觉heightForRowAtIndexPath: 方法是问题的一部分。不要执行计算,而是尝试在该方法中只使用两个常量:一个用于展开,一个用于折叠。我想知道是不是方法执行时间太长,然后认为行的高度是0,然后计算最终返回,但是UITableView滚动位置没有更新。

    另一个建议可能是您的 estimatedHeightheightFor 方法返回两个截然不同的值。

    【讨论】:

    • estimatedHeight and heightFor methods are returning two vastly different values.救了我!
    【解决方案2】:

    检查您是否在代码中使用了ScrollTORowAtIndexPAth someWhere

    【讨论】:

    • 不,我不使用那种方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    相关资源
    最近更新 更多