所以基本上你需要更新特定的单元格并增加它的高度。为此,您需要实现 UITextFieldDelegate 方法以确保在文本字段即将进入编辑模式时收到通知。
您还需要维护当前单元格的 indexPath 以增加特定单元格的高度。
@property(nonatomic, strong) NSIndexPath *selectedIndexPath;
将其初始值设置为nil
实现委托方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*)textField.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if( indexPath != nil) //Returns nil if cell is not visible
{
if( self.selectedIndexPath != nil )
{
//We need to reload two cells - one to hide UIButton of current cell, make visible the new UIButton for which textfield is being currently edited
NSIndexPath *previousIndexPath = self.selectedIndexPath;
self.selectedIndexPath = indexPath;
[self.tableView reloadRowsAtIndexPaths:@[indexPath, previousIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
else
{
//Reload the cell to show the button
self.selectedIndexPath = indexPath;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
重新加载单元格后,请确保使用 UITableViewDelegate 方法更新高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if( self.selectedIndexPath && [indexPath compare:self.selectedIndexPath] == NSOrderedSame)
{
return kHeightWithButton; //assuming its some constant defined in the code
}
return kHeightWithoutButton;
}
还取决于您如何处理 UITableViewCell 的约束,您可能需要调用单元格的 updateConstraints 方法。像be这样的示例代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell; //Assuming the you have some custom class to handle cell
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if( cell == nil )
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"myCell"];
//... code for initialization
cell.textField.delegate = self; //set the delegate to self so that we get delegate methods
}
if( self.selectedIndexPath && [indexPath compare:self.selectedIndexPath] == NSOrderedSame)
{
cell.button.hidden = NO;
}
else
{
cell.button.hidden = YES;
}
[cell updateConstraints];//update constraint to adjust the UIButton's visibility and constraints
//... other code if any
return cell;
}
希望对你有帮助!