2012-07-11 108 views
0

我遇到了与我的应用程序有关的问题,特别是与UITableView及其UITableViewCells。问题出在这里:我在UIView内创建了一个UITableView,并自定义了UITableViewCell。我已经涉及到两个控制器:IOS5 - 自定义UITableViewCell不显示内容

  • ListController.m
  • ListCellController.m

在IB,我联系了小区的项目我的头文件中手动创建IBOutlets单元控制器。

下面是该ListCellController.h/M代码:

// .h 
@class Item; 

@interface ListCellController : UITableViewCell 
@property (nonatomic, strong) Item *item; 
@property (nonatomic, strong) IBOutlet UILabel *itemName; 
@property (nonatomic, strong) IBOutlet UILabel *dates; 
@property (nonatomic, strong) IBOutlet UILabel *itemId;  
@end 

// .m 

// @synthetize stuff 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (!self) { 
     return nil; 
    } 

    self.textLabel.adjustsFontSizeToFitWidth = YES; 
    self.selectionStyle = UITableViewCellSelectionStyleGray; 

    return self; 
} 

- (void)setItem:(Item *)item 
{ 
    _item = item; 

    self.itemName.text = _list.itemName; 
    self.itemId.text = _list.itemId; 
    self.dates.text = [NSString stringWithFormat:@"Du %@ au %@", _list.date, _list.date]; 
} 

而这里的ListController.h/M代码:

// .h 
@interface ListController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    UITableView *cellList; 
} 
@property (strong, nonatomic) IBOutlet UITableView *cellList; 
@end 

// .m 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [_list count]; 
} 

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

    if (!cell) { 
     cell = [[ListCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.item = [_items objectAtIndex:indexPath.row]; 
    cell.itemId.text = cell.item.itemId; 
    cell.itemName.text = cell.item.itemName; 
    cell.dates.text = [NSString stringWithFormat:@"From %@ to %@", cell.item.date, cell.item.date]; 
    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [cellList deselectRowAtIndexPath:indexPath animated:YES]; 
} 

我的问题是,当我启动应用程序,我没有看到我的细胞。没有内容,没有错误,没有任何东西,空白单元格。

只要提一下,我也在这里使用故事板。

有没有人遇到过这个问题?我怎么能解决它?

谢谢。

+0

你链接的IB委托和数据源?我没有看到你在代码中设置委托。 – Eric 2012-07-11 17:40:59

+0

我只在IB中设置dataSource。看来我没有对代表做任何事情。但是我对IOS开发很陌生,在这种情况下我不知道该链接什么...... – 2012-07-11 17:53:34

+0

无论哪个头类包含您的tableView Outlet也需要委托集,与数据源的方式相同。 – Eric 2012-07-11 17:57:42

回答

4

如果您有UITableView的自定义单元格,你必须做这样的事情

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

    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ListCellController" owner:self options:nil]; 
     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (ListCellController *) currentObject; 
       break; 
      } 
     } 
    } 

    cell.item = [_items objectAtIndex:indexPath.row]; 
    cell.itemId.text = cell.item.segmentCode; 
    cell.itemName.text = cell.item.hotelName; 
    cell.dates.text = [NSString stringWithFormat:@"Du %@ au %@", cell.item.checkin, cell.item.checkout]; 
    return cell; 
} 
+0

这是IOS5和故事板的工作吗? – 2012-07-11 20:13:42

+0

@tsabz - 是否正在与故事板一起工作?你必须在提问中提到,因为如果你是这样的话,出列将总是返回一个单元格,并且你的所有单元格都将使用initWithCoder进行初始化。 – jrturton 2012-07-12 07:47:32

+0

是的,我正在使用故事板,我将在第一篇文章中指定它。 – 2012-07-12 07:48:30

相关问题