单击按钮(单元格中的按钮)时,应更改特定单元格

  • 本文关键字:按钮 单元格 单击 iphone ios
  • 更新时间 :
  • 英文 :


uiview上有一个表,我想在按下按钮时更改单元格高度,其他单元格的高度保持不变

通过委托方法将按钮按下事件传递给视图控制器,并按如下方式重新加载表视图。

[self.tableView reloadData];

在视图控制器(即表视图的数据源)中,实现heightForRowAtIndexPath方法并根据需要返回高度。

您可以在委托方法中更改单元格高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == clickedRow)
    return newHeitht;
return cellHeight;
}

您可以在按钮单击中设置一些条件,然后使用[tableView reloadData]重新加载表视图。将调用此函数。返回特定单元格的新高度。

-(void)buttonClick  {
[self.tableview reloadData];
selectedRow = //do something here 
}

以及在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…当您填充文本并将大小设置为零时,它将仅垂直拉伸标签,并强制宽度保持不变。4) 创建一个单独的函数(我的函数称为calculatedHeight),该函数返回一个浮点值,并返回您希望单元格在表中的高度(基于步骤3中重新定位的对象)。

- (float)calculatedHeight {
return textLabel.frame.origin.ytextLabel.frame.size.height5;
}

5) 在UITableView类中,您需要导入tableViewCell类并创建一个伪单元格对象。你将使用这个类来计算每个单元格需要有多高。然后在heightOfRowAtIndex方法中。。。。

- (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;
}

这是一个非常简单的例子,所以您可能需要在特定用途中疯狂地进行错误检查,但这至少应该为您指明正确的方向。享受

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    
    if (isSearching && indexPath.row == selectedIndex) {
        return 110;
    }
    else {
        return rowHeight;       
    }
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];

然后

[self.tableView beginUpdates];
[self.tableView endUpdates];

最新更新