【问题标题】:Increase table row height dynamically动态增加表格行高
【发布时间】:2017-09-11 05:18:05
【问题描述】:

我有一个表格,其中包含一个自定义单元格、一个标签和一个文本字段。我重新使用了大约 5 行的同一个单元格。只有当用户在文本字段上输入时,我才想在我的文本字段下方显示一个视图。当我在第一个文本字段视图中输入数据时,应该隐藏其他行和所有其他行。默认高度必须是 120,当我显示我的视图行高时应该增加到 250。这是我的代码,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

      return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"DetailsTableCell";
cell = (NewsDetailsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[NewsDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.row == 0) {
    cell.newView.tag = 200;
}
else if (indexPath.row == 1) {
    cell.newView.tag = 201;
}
else {
    cell.newView.tag = 202;
}

   return cell;
}

这里的新视图是我的 UIView,它在文本更改时显示。

- (IBAction)textFieldChanged:(UITextField *)textField replacementString:(NSString *)string {

NSIndexPath *indexPath = [newsTable indexPathForCell:(NewsDetailsTableViewCell*)[[textField superview] superview]];
NSIndexPath *myPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
NewsDetailsTableViewCell *newsTableCell = (NewsDetailsTableViewCell*)[newsTable cellForRowAtIndexPath:myPath];

 if (indexPath.row == 0 && newsTableCell.newView.tag == 100) {
    newsTable.rowHeight = 250;
    newsTableCell.newView.hidden = false;

} else if (indexPath.row == 1 && newsTableCell.newView.tag.tag == 101) {
    newsTable.rowHeight = 250;
    newsTableCell.newView.hidden = false;
} else {
    newsTable.rowHeight = 250;
    newsTableCell.newView.hidden = false;
   }
}

在我的文本更改中获得了我的视图,但行高没有改变。而且它仍然显示 120 高度。

我也试过这个,

NSArray* rowsTobeReloaded = [NSArray arrayWithObjects:indexPath, nil];
[newsTable reloadRowsAtIndexPaths:rowsTobeReloaded withRowAnimation:UITableViewRowAnimationNone];

但它没有用。我应该在文本更改中再次重新加载我的表格视图吗?还是我错过了什么?

【问题讨论】:

标签: ios objective-c uitableview heightforrowatindexpath


【解决方案1】:

你必须修改下面方法中的逻辑:

可以根据要显示 textField 的行来修改 selectedRowNum。 此外,cellForRowAtIndexPath 不用于设置表格视图中的行高。每次调用reloadData或reloadIndexes,heightForRowAtIndexPath方法都会被调用。

【讨论】:

    【解决方案2】:

    你需要改变高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
      if (usingNewView){
         return 250;
      }
      return 120;
    }
    

    并在

    中设置使用NewView的逻辑
    - (IBAction)textFieldChanged:(UITextField *)textField replacementString:(NSString *)string
    

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 2019-08-11
      • 2018-06-01
      • 2016-04-26
      • 2018-01-09
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多