Ios 如何仅在TableView的选定单元格中显示标签?

Ios 如何仅在TableView的选定单元格中显示标签?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一张桌子。我在单元格中添加了一个名为“lbl1”的标签。我实现了“heightforrowatinexpath”方法。和Iam使用增加选定行的高度 [tblView beginUpdates]; [tblView endUpdates]; 然后我在扩展区域的单元格中添加一个标签“lbl2”,这个标签应该只在所选单元格中可见 这里“lbl2”在后台显示,即使未选择特定单元格。它看起来像是覆盖了“lbl1” 有没有办法获得正确的输出?如果要在所选单元格中插入标签,请将其插入到didselect

我有一张桌子。我在单元格中添加了一个名为“lbl1”的标签。我实现了“heightforrowatinexpath”方法。和Iam使用增加选定行的高度

[tblView beginUpdates];
[tblView endUpdates];
然后我在扩展区域的单元格中添加一个标签“lbl2”,这个标签应该只在所选单元格中可见

这里“lbl2”在后台显示,即使未选择特定单元格。它看起来像是覆盖了“lbl1”


有没有办法获得正确的输出?

如果要在所选单元格中插入标签,请将其插入到
didselectRowAtIndexPath
委托方法中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:indexPath];
    //increase the cell height and then do the following:
    UILabel *lbl2 = [[UILabel alloc]init];
    *lbl2.text = @"Some text";
    [currentCell addSubview:lbl2]; //remember to position the label properly.        
}
希望这将是您正在寻找的解决方案

编辑
如果要在选择另一行时删除标签,请保存以前选择的行的indexPath,下次删除标签时,请减小、更新高度,然后将选定的indexPath设置为变量,然后更新当前的indexPath。如下面的代码所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cpreviousCell = [tableView cellForRowAtIndexPath:previousIndexPath];
    //remove lbl2 and decrease the height of the cell if required
    UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:indexPath];
    //increase the cell height and then do the following:
    UILabel *lbl2 = [[UILabel alloc]init];
    *lbl2.text = @"Some text";
    [currentCell addSubview:lbl2]; //remember to position the label properly.
    //and finally set the indexPath
    previousIndexPath = indexPath;     
}

还可以保留对以前选择的indexPath的引用,然后重新加载选定单元格的indexPath和新选定单元格的indexPath

这(我认为)比抓住细胞本身要好,尤其是为了避免重新选择。 由于TableView会滚动,因此比较两个单元格以避免重新选择可能会失败,而比较indexath.row则可以

因此,您希望保留indexPath,如果有更改调用:

NSArray *array = [NSArray arrayWithObjects:prevIndexPath,newIndexPath,nil]; 
[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];   

当然,实际的标签添加/删除应该在cellForRowAtIndexPath方法中完成

请再加一点代码。