【问题标题】:How can I get a uitableViewCell by indexPath?如何通过 indexPath 获取 uitableViewCell?
【发布时间】:2011-01-23 23:23:46
【问题描述】:

如何通过indexPath 获得UITableViewCell?像这样:

我使用UIActionSheet,当我点击确认时UIButton我想更改单元格文本。

我定义为属性的 nowIndex

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSInteger section =  [nowIndex section];
        NSInteger row = [nowIndex row];
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        formatter.dateFormat = @"yyyy-MM-dd";

        if (section == 0)
        {
            if (row == 0)
            {

            }
            else if (row == 1)
            {
                UILabel *content = (UILabel *)[[(UITableView *)[actionSheet ] cellForRowAtIndexPath:nowIndex] viewWithTag:contentTag];
                content.text = [formatter stringFromDate:checkInDate.date];
            }
            else if (row == 2)
            {

            }
        }
    }
}

【问题讨论】:

    标签: iphone uitableview nsindexpath


    【解决方案1】:
    [(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex]
    

    会给你uitableviewcell。但我不确定你到底要什么!因为你有这段代码,但你仍然在问如何获得 uitableviewcell。更多信息将有助于回答您:)

    添加:这是一种替代语法,可以在没有强制转换的情况下实现相同的效果。

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
    

    【讨论】:

    • 技术上你想在你的 tableView 的dataSource 属性设置的任何地方调用它,但通常是self
    【解决方案2】:

    最后,我使用以下代码获取单元格:

    UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex];
    

    因为类扩展UITableViewController:

    @interface SearchHotelViewController : UITableViewController
    

    所以,self 是“SearchHotelViewController”。

    【讨论】:

    • 如果一个类 (..)View 是 UIViewController 的子类,那么命名一个类 (..)View 会让人感到困惑。
    【解决方案3】:

    这是从索引路径获取自定义单元格的代码

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
     YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath];
    

    对于斯威夫特

    let indexpath = NSIndexPath(forRow: 2, inSection: 0)
    let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest
    

    对于 Swift 4(对于 collectionview)

    let indexpath = NSIndexPath(row: 2, section: 0)
    let cell = self.colVw!.cellForItem(at: indexpath as IndexPath) as? ColViewCell
    

    【讨论】:

    • 在有两个自定义单元格的情况下,我需要知道在此索引路径上返回哪种类型的单元格。获取类型的最佳方法是什么?
    【解决方案4】:

    对于 Swift 4

        let indexPath = IndexPath(row: 0, section: 0)
        let cell = tableView.cellForRow(at: indexPath)
    

    【讨论】:

      【解决方案5】:

      您可以使用以下代码获取最后一个单元格。

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
      

      【讨论】:

        【解决方案6】:

        我不太确定你的问题是什么,但你需要像这样指定参数名称。

        -(void) changeCellText:(NSIndexPath *) nowIndex{
            UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag];
            content.text = [formatter stringFromDate:checkInDate.date];
        }
        

        【讨论】:

        • 嗨,大卫,我重写了这个问题,你能再看一遍吗
        【解决方案7】:

        斯威夫特 3

        let indexpath = NSIndexPath(row: 0, section: 0)
        let currentCell = tableView.cellForRow(at: indexpath as IndexPath)!
        

        【讨论】:

          【解决方案8】:

          斯威夫特

          let indexpath = IndexPath(row: 0, section: 0)
          if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> {
              cell.backgroundColor = UIColor.red
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-20
            • 1970-01-01
            • 2015-09-14
            • 1970-01-01
            • 1970-01-01
            • 2013-04-20
            • 2014-09-25
            • 1970-01-01
            相关资源
            最近更新 更多