运行时调整UITableView的单元格

3

我已经创建了一个UITableViewCell的子类。我的需求是当我点击单元格时,在单元格底部添加一个UIView并相应地调整单元格的高度。这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
MyCell* cell = (MyCell *) [self.table cellForRowAtIndexPath:indexPath]; 
UIView * options = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 27, cell.frame.size.width, 27)];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button2];
UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button3];
//did some layout calculation here to position the button
[cell addSubview:options];
}

这里有一个视频,可以说明问题。

我还尝试将单元格的accessoryView从我的自定义UIButton更改为普通的UITableViewCellAccessoryDisclosure,但是没有成功。

MyCell * cell = (MyCell *)[self.table cellForRowAtIndexPath:temp];
     [cell.accessoryView removeFromSuperview];
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

如果你好奇我在做什么,我正在尝试创建一个推特机器人uitableviewcell,在这里你可以按下单元格/行并呈现其他选项(转发等),同时它也以动画形式显示。

来自“相关”侧边栏的可能重复问题:带有动态单元格高度的UITableView - 我需要做什么来修复向下滚动? - jscs
不确定为什么您将其声明为重复项...那个问题并没有询问如何向单元格添加子视图并调整其大小。 - aherlambang
那个问题的主体似乎已经回答了你的问题。不过,我可能是错的,这就是自动生成的注释说“可能”的原因。 - jscs
2个回答

2

我认为如果你看一下视频就会明白了。在我执行addSubview之后,heightForRowAtIndexPath委托方法会被调用吗? - aherlambang
实际上,我看了你的视频。不,这个委托并不是通过简单地添加子视图来调用的。但是当您使用那些beginUpdates和endUpdates方法时,它确实被调用。 (请查看我的答案)在添加子视图时,只需包含它们即可。 - fscheidl

0
将控件放入另一个单元格中并使用。
insertRowsAtIndexPaths:withRowAnimation:   

展示它并且

deleteRowsAtIndexPaths:withRowAnimation: 

隐藏它。


我也考虑过这种方法,但是我的UITable数据源来自一个Message的NSArray,这意味着我需要向数据源中添加一些不是Message的内容?这样可以吗?然后我还需要修改cellForRowAtIndexPath来处理这种特殊情况,对吗? - aherlambang
基本上是这样的。只要控件没有被隐藏,你的tableView:numberOfRowsInSection:应该返回number_of_items + 1,而tableView:cellForRowAtIndexPath:应该返回适当的控件单元格。 - Alex Chugunov

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