Iphone 当单击按钮(单元格中的按钮)时,该特定单元格是否应更改?
uiview上有1个表格,我想在按下按钮时更改单元格高度,其他单元格的高度保持不变通过委托方法将按钮按下事件传递给视图控制器,并按如下方式重新加载表格视图Iphone 当单击按钮(单元格中的按钮)时,该特定单元格是否应更改?,iphone,ios,Iphone,Ios,uiview上有1个表格,我想在按下按钮时更改单元格高度,其他单元格的高度保持不变通过委托方法将按钮按下事件传递给视图控制器,并按如下方式重新加载表格视图 [self.tableView重载数据] 在视图控制器(即表视图的数据源)中,实现方法并根据需要返回高度 您可以在委托方法中更改单元格高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(inde
[self.tableView重载数据]代码>
在视图控制器(即表视图的数据源)中,实现方法并根据需要返回高度 您可以在委托方法中更改单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == clickedRow)
return newHeitht;
return cellHeight;
}
您可以在按钮单击中设置一些条件,然后使用[tableview reloadData]
重新加载tableview。将调用此函数。返回特定单元格的新高度
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];
然后
在UITableview数据源中
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row == selectedRow)
return selectedRowHeight;
else
return defaultHeight;
}
1) 创建自定义UITableViewCell类。
2) 按您认为合适的方式填充单元格。我自己使用“hydrateWithArray”类型的函数。
3) 填充数据后,使用“sizeToFit”函数调整元素的大小并重新定位元素,以强制标签符合您放入其中的任何内容的大小。protip:首先设置标签的边框,然后将行数设置为0。。。当您填充文本和sizetofit时,它将仅垂直拉伸标签,并强制宽度保持不变。
4) 创建一个单独的函数(我的函数称为calculatedHeight),该函数返回一个浮点值,并返回希望单元格位于表中的高度(基于步骤3中重新定位的对象)
5) 在UITableView类中,需要导入tableViewCell类并创建一个虚拟单元对象。您将使用这个类来计算每个单元需要有多高。然后用高度加权指数法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height;
if ( !dummyCell ) dummyCell = [[CustomCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"myCell"];
[dummyCell hydrateWithTweet:[tableArray objectAtIndex:indexPath.row]];
height = [dummyCell calculatedHeight];
if ( height == 0 ) height = 50;
return height;
}
这是一个非常简单的示例,因此您可能需要在特定用途中疯狂地进行错误检查,但这至少应该为您指明正确的方向。享受吧 您在哪里获得按钮按下事件?在UITableViewCell子类或视图控制器中?有自定义单元格,当按下按钮时,每个单元格上都有按钮,特定的单元格高度只会改变
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row == selectedRow)
return selectedRowHeight;
else
return defaultHeight;
}
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
else {
return rowHeight;
}
- (float)calculatedHeight {
return textLabel.frame.origin.ytextLabel.frame.size.height5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height;
if ( !dummyCell ) dummyCell = [[CustomCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"myCell"];
[dummyCell hydrateWithTweet:[tableArray objectAtIndex:indexPath.row]];
height = [dummyCell calculatedHeight];
if ( height == 0 ) height = 50;
return height;
}