使用XIB文件加载UITableViewCell子类

时间:2012-06-14 01:31:50

标签: iphone ios5 uitableview

我无法将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也是如此。

2 个答案:

答案 0 :(得分:4)

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

2)XIB:文件所有者类更改为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;
    }

希望可以帮助你:)

答案 1 :(得分:1)

在.h文件中,你写它。

IBOulet CustomTableViewCell *tableCell;

并连接到CustomTableViewCell的.xib文件中的文件所有者。

您可以在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

中修改此内容
if (cell == nil) 
 {
     [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
     cell = tableCell;
 }

我认为这会对你有所帮助。

相关问题