UITableView:如何在单击button时dynamic更改单元格高度?
我有一个UITableView,其中每个单元格都有一个button。
点击button时,单元格的高度将会改变。
当然,整个桌面的高度会根据它而改变。
在漫长的冲浪过程中,我找不到方法。 什么是实现这个优雅的解决scheme?
(看到这个答案,对Tapas Pal感谢您的原始答案,我只是改变了它以更好地适合您的问题。)
你将需要一个BOOL
variables告诉单元格,它的高度应该与其他的不同。 您还需要一个数字(这里我将使用NSInteger
)variables,以便您可以增加右侧单元格的高度。
BOOL shouldCellBeExpanded = NO; NSInteger indexOfExpandedCell = -1;
然后,在您的-tableView:cellForRowAtIndexPath:
方法中,将以下内容添加到devise单元格的位置。 您将设置button标签与其单元格的行相同,以便您知道要展开的单元格。 而且,如果您需要将任何元素添加到单元格中,则可以在if
语句中执行此操作。
[[cell aButton] setTag:[indexPath row]]; if(shouldCellBeExpanded && [indexPath row] == indexOfExpandedCell) { // design your read more label here }
移动到button的IBAction
。 轻按button时, UITableView
将重新加载button所在的单元格。 注意:如果您在UITableView
使用多个部分,则需要添加另一个数字variables来解决这个问题。 评论你是否需要帮助。
-(IBAction) aButtonTapped:(id)sender { UIButton *aButton = (UIButton *)sender; indexOfExpandedCell = [aButton tag]; shouldCellBeExpanded = YES; [[self tableView] beginUpdates]; [[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: indexOfExpandedCell inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; [[self tableView] endUpdates]; }
最后,在-tableView:heightForRowAtIndexPath:
方法中:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(shouldCellBeExpanded && [indexPath row] == indexOfExpandedCell) return 200.0f; //Your desired height for the expanded cell else return 100.0f; //The other cells' height }
这样做是检查一个单元格是否需要扩展,如果是的话,是否该单元格是当前被要求的高度值。 如果是,则返回值是展开的高度。 如果没有,原来的一个。
调用函数- reloadRowsAtIndexPaths:withRowAnimation:
实现函数UITableViewDelegate
函数- tableView:heightForRowAtIndexPath:
并返回新的高度。
- 在UITableViewCell中的UILabel大小不正确(分配文本后的animation)
- 如何在Xcode5中使用AutoLayout制作ScrollView
- UICollectionView单元格在删除具有estimatedItemSize的项目时resize
- 如何使用AFIncrementalStore在每个请求中添加一个Auth令牌?
- 什么时候在容器环境中调用viewDidLoad?
- 如何在通用的精灵套件游戏中调整精灵的大小
- 如何创build一个固定的导航栏iOS Swift
- 在iOS上,边距,边缘插入,内容插入,alignmentmatrix,布局边距,锚点之间有什么区别?
- 使用自动布局时,设置UICollectionViewFlowLayout的属性不起作用