我有一个表格 View ,并且使用两个自定义单元格。在自定义单元格中,我设置了 uilabel 并隐藏。现在,当用户从 TableView 中选择一个单元格时,在 didSelectRowAtIndexPath 方法中,我想显示该标签。我尝试使用 follow,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
HoteldetalcelloneTableViewCell *cellone = [tableView dequeueReusableCellWithIdentifier:@"cellone"];
HoteldetailcelltwoTableViewCell *celltwo = [tableView dequeueReusableCellWithIdentifier:@"celltwo"];
if(indexPath.section == 0)
{
HotelDetailsone *secone = [roomonearray objectAtIndex:indexPath.row];
if([secone.offerallow isEqualToString:@"True"])
{
celltwo.selectedsignLabel.hidden = NO;
}
else
{
cellone.selectedsignLabelone.hidden = NO;
}
NSLog(@"price for room 1 : %@", secone.itempriceText);
}
else
{
HotelDetailsone *sectwo = [roomtwoarray objectAtIndex:indexPath.row];
NSLog(@"price for room 2 : %@", sectwo.itempriceText);
}
}
注意:我使用了断点并进行了检查,它导航到正确的语句。但没有任何反应
我也尝试过以下,
[cellone.selectedsignLabelone setHidden:NO];
但什么也没发生。希望你能帮忙解决这个问题。谢谢。
最佳答案
UITableView
重新使用单元格以节省内存:当您调用 dequeueReusableCellWithIdentifier:
时,您会得到一个单元格对象,该对象将在下一次表格 View 将被更改时更改需要绘制一个单元格,所以当你改变它时,它会被丢弃。
实际加载和显示的单元格对象在 cellForRowAtIndexPath:
方法中返回。尝试修改 cellForRowAtIndexPath
中的单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellone"];
if (indexPath == selectedIndexPath ) // this is the selected cell
{
cell.selectedsignLabelone.hidden = NO;
}
return cell;
}
如果您想在 didSelectRowAtIndexPath:
方法中更新您的单元格,请尝试在最后使用 [tableView reloadData]
并替换 dequeueReusableCellWithIdentifier:
使用 cellForRowAtIndexPath
。
关于ios - 为什么我不能在 tableview、iOS、objective c 中的 didSelectRowAtIndexPath 中隐藏/显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38201059/