【问题标题】:Loading a UITableViewCell subclass using a XIB file使用 XIB 文件加载 UITableViewCell 子类
【发布时间】:2012-06-17 01:00:25
【问题描述】:

我无法让我的CustomTableViewCell(UITableViewCell 的子类)出现在我的表格视图中。

我使用 xib 来表示该单元格,但我假设数据源委托的代码不会更改。我确保在表格视图单元格 XIB 中设置了相同的重用标识符。

我将问题隔离为返回表格单元格的数据源方法无法正常工作,这里是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    DataObject *foo = [self.dataArray objectAtIndex:indexPath.row];

    if (cell == nil) 
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [[cell overview] setText:foo.overview];
    [[cell price] setText:foo.price];
    NSLog(@"cell initialized, description text is %@",cell.overview.text);

    return cell;
}

不知道为什么这不起作用,但最后一条日志语句总是在最后打印一个 (null),是的,我确实验证了数据对象的 overview 属性中有一个有效的字符串。 price 也一样。

【问题讨论】:

  • stackoverflow.com/questions/4195726/… 在这里试试!!!我想你的答案就在那里!!!
  • 是的,它有效,但是当我第一次尝试该方法时抛出异常,请参阅我对已接受答案的评论以了解如何避免。
  • 嘿@AndrewLauerBarinov,所以我也遇到了同样的问题,但我使用的是故事板。想知道您对已接受答案的评论,我似乎不理解您所说的“IBOutlet 连接是针对视图而不是文件所有者”的声明。抱歉,我对 ios 有点陌生,所以我仍在努力学习我需要在连接和东西方面做的事情。你能再解释一下吗?谢谢!还在这里发布了一些东西stackoverflow.com/questions/15175472/…
  • 当您在 IB 中建立连接时,您通常在文件所有者(通常是 UIViewController 或子类)中进行,在这种情况下,您希望建立与视图的连接,而不是可以找到在框内标记为视图控制器场景(从 xcode 4.6 开始)

标签: iphone ios5 uitableview


【解决方案1】:

1) 将您的自定义单元格类分开

2) XIB : File Owner 类更改为 NSObject

3) XIB : UITableViewCell 更改为 MyTableViewCell

4) 要在表格视图代码中添加的位置为

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"MyCell";

    MyTableViewCell *cell = (MyTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *arrayCellXib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" 
                                                              owner:self 
                                                            options:nil];
        if (indexPath.row == 0) {
            // first table cell
            cell = [arrayCellXib objectAtIndex:0]; // *
            ...... 
         }else {

            cell = [arrayCellXib objectAtIndex:1]; // *
         }
        ...
      }
   return cell;

}

索引 0,1 处的对象 ... 是您在 xib 中创建 MyTableViewCell 的顺序的索引,如果您想拥有多个自定义单元格。意味着在 MyTableViewCell 类中,您可以制作无限的自定义单元格并根据要求使用。

通常,只需在 XIB 中创建一个自定义单元格并执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    int thisRow = indexPath.row;
    NSString *exampleText = [yourData objectAtIndex:thisRow];

    static NSString *CellID = @"cu5";

    FancyCell *cell =
       (FancyCell *)[tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil)
        {
        NSArray *cellTeam =
          [[NSBundle mainBundle] loadNibNamed:@"FancyCell"
             owner:self options:nil];
        cell = [cellTeam objectAtIndex:0];
        }

    [cell someFunction:exampleText];
    [cell.someLabel setText:exampleText];
    return cell;
    }

希望对你有帮助:)

【讨论】:

  • 这是一个真正有用的答案,非常感谢。这正是使用 CUSTOM XIB 和 CUSTOM CLASS 的方法。如果您想“只是”使用自定义 xib(不需要自定义类),请在此处使用 Jamihash 的答案:stackoverflow.com/questions/15378788/…
【解决方案2】:

在.h文件中,你写。

IBOulet CustomTableViewCell *tableCell;

并连接到 CustomTableViewCell 的 .xib 文件中的 File's Owner。

你在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath修改这个

if (cell == nil) 
 {
     [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
     cell = tableCell;
 }

我认为这对你会有帮助。

【讨论】:

  • 我想补充的一件事是确保您的 IBOutlet 连接到视图而不是文件的所有者(如果这样做,您会得到与键值相关的异常)
  • 每当我在我的 viewcontroller.h 上放置一个属性时也会得到这个...连接“”不能有一个原型对象作为它的目的地。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 2017-01-08
相关资源
最近更新 更多