我有一个表,其中一个自定义单元格包含一个label和一个textfield.Am,该单元格重用了具有大约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];但它并没有起作用。我是否应该在文本更改中重新加载我的表视图?还是我错过了什么?
发布于 2017-09-11 13:22:39
YOu需要更改高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (usingNewView){
return 250;
}
return 120;
}并在中设置usingNewView逻辑
- (IBAction)textFieldChanged:(UITextField *)textField replacementString:(NSString *)string发布于 2017-09-11 13:50:30
您必须修改以下方法中的逻辑:

可以根据要在其中显示textField的行来修改selectedRowNum。此外,cellForRowAtIndexPath不用于设置表视图中的行高。每次我们调用reloadData或reloadIndexes时,heightForRowAtIndexPath方法都会被调用。
https://stackoverflow.com/questions/46148622
复制相似问题