如何dynamic扩展tableview中的单元格?
我只有一个cell
,在tableview
有一个button。 cell
高度定义在
tableView:tableView:heightForRowAtIndexPath:
细胞高度是40;
当我点击button时,如何将单元格高度更改为80? 谢谢你的帮助。
定义一个属性和@synthesize
它:
@property (retain, nonatomic) NSIndexPath* selectedRowIndex;
覆盖didSelectRowAtIndexPath
和heightForRowAtIndexPath
方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(selectedRowIndex && indexPath.row == selectedRowIndex.row) { //[_yourTableView deselectRowAtIndexPath:indexPath animated:YES]; If you only want the cell to get bigger selectedRowIndex = nil; } else { self.selectedRowIndex = [indexPath retain]; } [tableView beginUpdates]; [tableView endUpdates]; } - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (tableView == _yourTableView) { if(selectedRowIndex && indexPath.row == selectedRowIndex.row){ return 80; } // Cell isn't selected so return single height return 40; } else return 0; }
由于beginUpdates和endUpdates行,这应该animation的单元格高度。
这里是UITableView委托的文档 。 你需要设置一个委托并设置函数是 – (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath。 记住返回值是CGFloat,所以返回80.0而不是80.希望这有助于..阅读更多关于其他代表函数在文档中…
当button被按下时,你应该设置一个variables来表示它已被按下,然后在你的tableView上调用reloadData。 这将使tableView:heightForRowAtIndexPath被再次调用。
在tableView:heightForRowAtIndexPath中检查告诉你button是否被按下的variables,如果已经按下了,返回80
- 在Cocoa Touch Framework中包装静态库
- UIWebview启用cookie
- 如何在iOS中使用URL和NSString在UIImageView上显示图像?
- NSStringvalidation
- 如何在iPhone上使用OpenGL ES绘制实心圆?
- 我什么时候需要担心iOS应用程序中的线程安全?
- 检查用户位置是否在形状内
- 应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用时不在UIApplicateStateBackground中
- Xcode的 – 我的应用程序崩溃,错误是“无效指针从空闲列表中出列***在malloc_error_break设置断点debugging”