【问题标题】:Increase height of a cell when textfield in it begins editing当其中的文本字段开始编辑时增加单元格的高度
【发布时间】:2017-06-10 06:57:20
【问题描述】:

我有一个tableview,其中一个单元格包含一个textfield 和一个button。我只想在textfield 激活时显示带有button 的区域。

因此,当时我想提供一个不同的height,以便textfield 仅可见,当textfield 变为活动状态时,我想增加height of the tableviewcell 以显示button。请帮忙。

我的牢房是这样的。

当未选择文本字段时,我希望它看起来像:

选择文本字段时,我希望它看起来像:

它与“在 UITableView 中使用自动布局进行动态单元格布局和可变行高”无关,因为我想根据所选的文本字段确定高度。

【问题讨论】:

  • 问题不清楚。
  • 也添加您的代码。
  • 您是否尝试过重新加载单元格?
  • @AnoopNyati:我从哪里重新加载单元格?重新加载它们不会改变高度,因为我还没有在任何地方更新高度。
  • 既然你想增加高度,你必须配置高度。您是否可以使用 tableView:heightForRowAtIndexPath: 来定义高度?

标签: ios swift xcode uitableview row-height


【解决方案1】:

所以基本上你需要更新特定的单元格并增加它的高度。为此,您需要实现 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;
}

希望对你有帮助!

【讨论】:

  • 我已经使用单元格中的自定义委托实现了类似的功能。我的方法是相似的,所以一个赞成和接受的答案。我正在使用 Swift 3。感谢您提供如此详细的说明并抽出时间来dis。非常感谢。如果您觉得我的问题合适,请点赞。
猜你喜欢
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 2012-01-19
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多