【问题标题】:iOS TableView how to access custom cell variablesiOS TableView如何访问自定义单元格变量
【发布时间】:2013-12-22 22:59:06
【问题描述】:

我有一个表格视图

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    **NSString *channelID = [object objectForKey:@"channelID"];**

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil)
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle 
                                     reuseIdentifier:CellIdentifier];

}

我正在像这样访问一个表格视图单元格:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (NSIndexPath *path in [tableView indexPathsForVisibleRows]) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    }
    //I want to access cell.ChannelID //
}

如何访问 channelID 变量? 当用户停止滚动时,我想访问可见的单元格和自定义变量。

谢谢

【问题讨论】:

    标签: ios objective-c uitableview tableview


    【解决方案1】:

    如果你想要一个自定义的 UITableView 单元格,那么你需要创建一个 UITableView 单元格的子类。从那里,您需要在此自定义单元格上为您的对象声明一个属性。确保公开声明此属性。从我在您的代码中看到的情况来看,它可能应该是这样的。

    @interface MyCustomCell : UITableViewCell
    
    @property (strong, nonatomic) NSDictionary *myObject;
    
    @end
    

    从那里,您需要将单元格子类的标头导入表视图委托/数据源方法使用的实现文件中,而不是在 cellForRowAtIndexPath 中引用 UITableViewCell:引用您的子类,然后为您的子类设置一个值新添加的属性。

    - (MyCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (!cell) {
            cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
    
        NSString *channelID = [cell.myObject objectForKey:@"channelID"];
    
        [cell.textLabel setText:channelID];
    
        return cell;
    }
    

    当然,您首先需要提供逻辑来传播此字典,这是不言而喻的。然后就滚动视图委托方法而言,您遇到了类似的问题。您无法访问 UITableViewCell 基类上的自定义对象。您再次需要引用您的子类。这可以通过简单的演员轻松完成。

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        for (NSIndexPath *path in [tableView indexPathsForVisibleRows]) {
            MyCustomCell *cell = (MyCustomCell *)[self.tableView cellForRowAtIndexPath:path];
    
            NSString *channelID = [cell.myObject objectForKey:@"channelID"];
            //
        }
    }
    

    【讨论】:

      【解决方案2】:

      0x7fffffff 答案有效。您也可以使用此功能并跳过空值检查。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
      
          NSString *channelID = [cell.myObject objectForKey:@"channelID"];
      
          [cell.textLabel setText:channelID];
      }
      

      但是你必须通过在 viewDidLoad 中注册来声明什么是“CustomCell”,否则你会得到一个异常:

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
      }
      

      我更喜欢这个,因为它使dequeueReusableCellWithIdentifier: forIndexPath: 保证返回一个单元格,并使cellForRowAtIndexPath 更干净一些。但两者都同样有效。

      【讨论】:

        【解决方案3】:

        你确定 channelID 是 UITableViewCell 的成员吗?如果你已经继承了 UITableViewCell 并且正在使用你自己的自定义 tableview 单元格,你想使用那个类

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-21
          • 1970-01-01
          • 2017-09-09
          • 1970-01-01
          • 2011-04-13
          • 1970-01-01
          相关资源
          最近更新 更多