【问题标题】:what are issues involved allocating and initializing subclass of UITableViewCell分配和初始化 UITableViewCell 的子类有哪些问题
【发布时间】:2014-03-26 11:31:40
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    cell.firstLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    cell.secondLabel.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row];

    return cell;
}

这是来自 Apple Table View Programming Guide 的代码 sn-p

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 工作正常,无需检查 nil,因为单元格是在故事板中定义的,并且始终返回有效单元格。

但如果我不使用故事板,我将如何以编程方式在我的表格视图中使用多个自定义单元格?以及涉及到哪些问题allocating and initializing MyTableViewCell

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    你应该使用方法

    - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
    - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
    

    UITableView。你可以阅读文档here

    当你调用方法时

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
    

    ,它检查重用队列中是否有可用的单元格。如果没有,它会检查它是否可以自动创建这个单元格。如果您之前为此重用标识符注册了单元格类或 nib,它将使用类或 nib 创建新单元格并返回它。如果你没有注册任何东西,它会返回 nil。

    最好使用注册,因为如果您有不同的自定义单元格用于不同的重用标识符,则创建这些单元格的代码会变得混乱。这也是正确的方法。 iOS5和iOS6分别增加了注册方法。程序员创建自定义cell的代码与iOS老版本有关。

    【讨论】:

    • 请您详细说明这些方法是如何工作的,为什么不分配 uitableviewcell 的 init 子类,以及如何找到这些方法:p
    【解决方案2】:

    如果您想以编程方式创建单元格,则需要像这样分配和初始化表格单元格。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    static NSString *identifier = @"cell";
    
        UITableViewCell *cell = [listtableview dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    
        if (cell ==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        }
    }
    

    【讨论】:

    • 我正在使用自定义单元格 MyTableViewCell,而不仅仅是常规单元格。
    • ok 没问题 :) 在这种情况下,您必须创建一个自定义单元格的 saprate 类,请在此处查看此链接,您可以找到我们如何添加自定义单元格mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB
    • 我想以编程方式完成所有工作,没有 NIB 或情节提要。
    • 好的,如果你应该在自定义单元格类中以编程方式添加你的单元格元素(标签,图像)。检查此以获取更多详细信息stackoverflow.com/questions/18969355/…
    【解决方案3】:

    您也可以通过这种方式使用自己的自定义单元格

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"cell";
    
        MyTableViewCell *cell=(MyTableViewCell *)[self.yourtableview  dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:0];//change as per your need
    
       if(cell==nil)
        {
        [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
        cell=self.mytableviewcellref;
        }
        cell.textLabel.text=@"sometext";
        return cell;
    }
    

    希望对你有帮助..

    【讨论】:

    • @SJ。您需要在使用自定义单元格时加载 Nib。
    • 请告诉我为什么你没有分配 uitableviewcell 的 init 子类以及这个 loadNibNamed 是如何工作的?
    • 您正在将自定义单元格直接在其 cell.xib 处连接到您的视图控制器。所以你可以在这里加载它的笔尖。如果您使用的是默认 UITableViewCell,那么如果它是 nil..@SJ..,则必须分配才能使用它
    • 我有 uitableviewcell 的子类,它也有 nib,我想创建 5 个不同类型的单元格,我应该创建 5 个不同的 uitableviewcell 子类吗?
    • 您需要为自定义单元格创建 5 个不同的 xib,并使用 [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil]; ///根据 xib 名称更改 MYTableViewCell。对于每个索引路径
    【解决方案4】:

    如果您不使用情节提要,则需要检查单元格是否为 nil,如果是,则分配一个新单元格。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = @"MyIdentifier";
        MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil)
        {
            cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    
        cell.firstLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
        cell.secondLabel.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row];
    
        return cell;
    }
    

    【讨论】:

      【解决方案5】:

      你可以使用另一种方法:

      MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"> forIndexPath:indexPath];
      

      您传递了额外的参数 - indexPath。 之后检查单元格是否为 nil,如果是,则分配并初始化它。

      【讨论】:

      • 我没有看到 MyTableViewCell 的分配和初始化。它将如何运作?
      • 你检查它是否为 nil,然后你分配并初始化它。
      猜你喜欢
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2020-10-17
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多