ios - 为什么我不能在 tableview、iOS、objective c 中的 didSelectRowAtIndexPath 中隐藏/显示标签

标签 ios objective-c uitableview hidden didselectrowatindexpath

我有一个表格 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/

相关文章:

ios - 水平 scrollView 不滚动

ios - 如何通过引用将枚举传递给方法

ios - 使用和不使用图像动态调整 TableViewCell 的大小

ios - 如何根据 "if"条件创建对象的不同子类

ios - 从 Parse 下载后使用图像或文本进行排序

objective-c - 分组 TableView 最后一个单元格边框

ios - 在 Stackview 中设置项目的 ID

ipad - UIWebView 抛出 NSUnknownKeyException iPad

objective-c - 将 NSArray 的计数与整数值进行比较会导致错误的代码执行

ios - 当键盘处于事件状态时,如何阻止 UITableView 在重新加载时不恰本地更改弹出窗口中的大小?