0

我有一个 UITableView,里面有 16-20 个单元格,单元格大小是动态的。当一个单元格自行扩展时,它也应该将自己移动到屏幕顶部。我使用“UITableView setContentOffset”方法做到了这一点。除了表格中的最后一个单元格之外,它运行良好,它无法将自身移动到顶部。

我尝试更改 UITableView 的框架和内容大小,但没有一个对我有用!

任何想法?

[更新] 这是代码的一部分:(它在 UITableViewCell 内,所以 self 指向当前单元格)

HomeViewController *tempViewController = (HomeViewController *) delegate;
UIView *commentField;

/*Skipping lines of codes manipulating commentField */

//Adding a subview to current cell which needs more space
[self addSubview:commentField]; 

//Expanding cellSize to EXPANDED_CELL_HEIGHT
//ViewController has access to cell size property and using that to determine each cell size. 
[self setCellSize:(EXPANDED_CELL_HEIGHT)]; 

//Reloading UITableView to reflect the cell size change with animation
[[tempViewController tableView] beginUpdates];
[[tempViewController tableView] endUpdates];

[[tempViewController tableView] setContentOffset:CGPointMake(0, self.frame.origin.y) animated:YES];    

在我的视图控制器中(正如我之前所说)我得到 cellSize 表单单元格本身

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [(BaseTableViewCell *)[cellContainer objectAtIndex:indexPath.row] cellSize];
}
4

1 回答 1

3

您可以使用 tableview 委托方法更改单元格的高度heightForRowAtIndexPath:当 indexPath.row 等于您的最后一行时,返回您想要的高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == i)//i being whatever row index you want to change
    {
      return 60.0;//or float size you want
    }
    else
    {
     return 30.0;
    }
}

或者您可以只修改内部单元格的边界willDisplayCell:。如果您使用自定义表格视图单元格,只需将子视图缩小到您想要的任何框架,并使单元格背景清晰等。

于 2013-05-03T22:19:42.830 回答