首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态增加表格行高

问动态增加表格行高
EN

Stack Overflow用户
提问于 2017-09-11 13:18:06
回答 2查看 255关注 0票数 0

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

代码语言:javascript
复制
- (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,它在文本更改时显示。

代码语言:javascript
复制
- (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的高度。

我也试过了,

代码语言:javascript
复制
NSArray* rowsTobeReloaded = [NSArray arrayWithObjects:indexPath, nil];
[newsTable reloadRowsAtIndexPaths:rowsTobeReloaded withRowAnimation:UITableViewRowAnimationNone];

但它并没有起作用。我是否应该在文本更改中重新加载我的表视图?还是我错过了什么?

EN

回答 2

Stack Overflow用户

发布于 2017-09-11 13:22:39

YOu需要更改高度

代码语言:javascript
复制
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


  if (usingNewView){
     return 250;
  }
  return 120;
}

并在中设置usingNewView逻辑

代码语言:javascript
复制
- (IBAction)textFieldChanged:(UITextField *)textField replacementString:(NSString *)string
票数 0
EN

Stack Overflow用户

发布于 2017-09-11 13:50:30

您必须修改以下方法中的逻辑:

​

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46148622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档