【问题标题】:How to reach the current selected cell in didSelectRowAtIndexPath?如何在 didSelectRowAtIndexPath 中到达当前选定的单元格?
【发布时间】:2011-09-26 16:27:19
【问题描述】:

我想通过方法更改单元的附件视图类型:didselectrowatindexpath,即选择了一行时,我想更改附件视图类型,我可以在该方法内部吗?

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    您可以像这样使用 indexPathdidSelectRowAtIndexPath: 方法中获取单元格。

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    

    【讨论】:

    • 当我使用您的解决方案制作 [cell setAccessoryType: UITableViewCellAccessoryDe​​tailDisclosureButton] 时,它对其附件视图没有影响,有什么问题?
    • @JaHelia,您是否使用[tableView reloadData]didSelectRowAtIndexPath: 方法中重新加载表视图?
    • 我认为您必须以某种方式保存索引路径,然后在 cellForRowAtIndexPath 中设置附件视图。否则,每次 tableview 向代表询问单元格时,它都会被覆盖。
    • @JaHelia,是的。您应该按照上面提到的@Tobi 进行操作,以使单元格即使在重新加载表格视图后也显示附件视图
    • 你也可以在单元格上使用 setNeedsLayout
    【解决方案2】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    {
         UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
         // Access accessory View  as below.  
         UIView * myCellAccessoryView = myCell.accessoryView;
    }
    

    【讨论】:

      【解决方案3】:

      使用以下函数并传递所选行的索引路径,以便再次重新加载特定单元格。

      - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
      

      另一种解决方案:存储选定的行索引并重新加载 tableview。然后在cellForRowAtIndexPath 中检查选定的行并更改单元格的附件视图。

      【讨论】:

        【解决方案4】:

        斯威夫特

        您可以使用 indexPath 从 didSelectRowAtIndexPath: 方法获取单元格,如下所示:

         let cell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
        

        【讨论】:

          【解决方案5】:

          存储选定的索引路径。例如: NSInteger selectedIndexPath; 然后按照以下步骤操作。设置你的附件视图。

          -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
           {
              AddressTableViewCell *cell=(AddressTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
          
              cell.backgroundColor=[UIColor lightGrayColor];
          
              Address *addr=[self.locationArray objectAtIndex:indexPath.row];
          
              cell.titleLabel.text = addr.addressTitle;
              cell.detailLabel.text = addr.addressDetail;
          
                  if (indexPath.row == selectedIndexPath) {
                      [cell.selectButton setImage:[UIImage imageNamed:@"checkboxFilled"] forState:UIControlStateNormal];
                  }
                  else {
                      [cell.selectButton setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
          
                  }
          
              return cell;
          }
          
          
          -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
          
              selectedIndexPath=indexPath.row;
          
              [self.tableView reloadData];
          
          }
          

          【讨论】:

            【解决方案6】:

            在 Swift 3.0 中:您可以根据需要使用这两种方式

             let cell:UITableViewCell = tableView.cellForRow(at: indexPath) as UITableViewCell
            

            let cell:CustomTableViewCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
            

            编码愉快!!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2023-03-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-02-16
              相关资源
              最近更新 更多