【问题标题】:adjusting UITableView's cell at runtime在运行时调整 UITableView 的单元格
【发布时间】:2011-05-15 16:40:03
【问题描述】:

我对 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];
}

这是一个说明问题的video

我还尝试将我的单元格的附件视图从我的自定义 UIButton 更改为常规 UITableViewCellAccessoryDisclosure,但它没有工作

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

如果您对我想要做什么感到好奇,我正在尝试创建一个 tweetbot uitableviewcell,您可以在其中按下单元格/行,它会为您提供其他选项(转推等),并且还会显示动画。

【问题讨论】:

标签: iphone objective-c ipad uitableview


【解决方案1】:

我不知道我是否正确理解了您要执行的操作。但是,如果您确实想为 UITableViewCell 的高度变化设置动画,请尝试在 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 内调整它并调用

[tableView beginUpdates];
[tableView endUpdates];

【讨论】:

  • 我想如果你看视频你就会明白。在我执行 addSubview 之后会调用 heightForRowAtIndexPath 委托吗?
  • 其实我看过你的视频。不,这个委托不是通过简单地添加一个子视图来调用的。但实际上,当您使用这些 beginUpdates 和 endUpdates 方法时,它会被调用。 (看我的回答)在添加子视图时简单地包括它们。
【解决方案2】:

将控件放入另一个单元格并使用

insertRowsAtIndexPaths:withRowAnimation:   

显示它和

deleteRowsAtIndexPaths:withRowAnimation: 

隐藏它。

【讨论】:

  • 我也想过这种方法,但是我的 UITable 数据源来自消息的 NSArray,这意味着我需要向数据源添加不是消息的东西吗?这样好吗?然后我还需要修改 cellForRowAtIndexPath 来处理这种特殊情况,对吗?
  • 基本上,是的。每当控件未隐藏时,您的 tableView:numberOfRowsInSection: 应该返回 number_of_items + 1 并且 tableView:cellForRowAtIndexPath: 应该返回适​​当的控件单元格。
猜你喜欢
  • 2019-08-15
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多