2012-06-14 42 views
3

我无法让我的CustomTableViewCellUITableViewCell的子类出现在我的表格视图中。使用XIB文件加载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同样的事情。

+1

http://stackoverflow.com/questions/4195726/how-to -create-custom-tableviewcell-from-xib试试!!!我认为你的答案在那里! – trumpetlicks

+0

是的,它可以工作,但是当我在前几次尝试该方法时遇到了异常,请参阅我对如何避免接受的答案的评论。 –

+0

嘿@AndrewLauerBarinov,所以我在同一个问题atm,但我用故事板代替。想知道您对接受答案的评论,我似乎并不明白“IBOutlet与您所说的视图不是文件所有者”的关系。对不起,我有点新与ios,所以我仍然试图学习我需要做的连接和东西方面的​​东西。你能请进一步解释一下吗?谢谢!还在这里发布的东西http://stackoverflow.com/questions/15175472/custom-uitableviewcell-will-not-display-label-texts – gdubs

回答

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; 
} 

我认为这将是对你有所帮助。

+0

我想补充的一件事是确保你的IBOutlet连接是对视图进行的,而不是文件的所有者(如果你这样做,你会得到一个关键值相关的异常) –

+0

当我把一个属性放在我的viewcontroller.h ...连接“”不能有一个原型对象作为其目的地。 – gdubs

5

1)您的自定义单元格类单独

2)厦门国际银行:文件所有者类更改为NSObject的

3)厦门国际银行:UITableViewCell中更改为要里面添加MyTableViewCell

4)表视图代码作为指数0,1

- (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; 

} 

对象...是指数,其命令你做MyTableViewCell在厦门国际银行,如果你想有更多的塔n一个自定义单元格。意味着在MyTableViewCell类中,您可以根据需要制作无限定制单元格和使用。

通常情况下,只要在厦门国际银行ONE自定义单元格,并做到这一点:

- (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; 
    } 

希望能可帮助ü:)

+0

这是一个真正有用的答案,非常感谢。这正是如何用CUSTOM XIB和CUSTOM CLASS来做到的。如果你想“JUST”使用定制的xib(不需要定制类),请使用Jamihash的答案:http://stackoverflow.com/questions/15378788/how-can-i-use-custom-uitableviewcell -and-UITableView的功能于同一厦门国际银行 – Fattie