2014-05-21 161 views
1

所以我创建了一个自定义单元格(使用.xib文件)并使用自定义控制器类将其链接起来,并且我也不忘写入单元格标识符。我也在我的表视图控制器原型单元格中给出了相同的单元格标识符。在自定义单元控制器类中,我只有一个出口到.h文件中的文本标签。下面是我的表视图控制器的代码。当我运行应用程序时,自定义单元格不显示,但有单元格,因为我可以点击它们(但它们只是白色)。我做错了什么,为什么不显示自定义单元格?自定义单元格不显示

如果我使用默认的单元格(不是自定义),那么一切工作正常。所以问题是我没有正确使用我的自定义单元格。

#import "ListmaniaTableViewController.h" 
#import "ListmaniaTableViewCell.h" 

@interface ListmaniaTableViewController() 

@property (strong, nonatomic) NSMutableArray *tasks; //of task object 

@end 

@implementation ListmaniaTableViewController 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    task *task1 = [[task alloc] init]; 
    task1.taskDescription = @"TASK 1"; 
    [self.tasks addObject:task1]; 
    task *task2 = [[task alloc] init]; 
    task2.taskDescription = @"TASK 2"; 
    [self.tasks addObject:task2]; 
    task *task3 = [[task alloc] init]; 
    task3.taskDescription = @"TASK 3"; 
    [self.tasks addObject:task3]; 
} 

- (NSMutableArray *)tasks{ 
    if(!_tasks){ 
     _tasks = [[NSMutableArray alloc] init]; 
    } 
    return _tasks; 
} 

/*- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 

    } 
    return self; 

}*/ 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"Add New Item"]) { 

    } 
} 

- (void)addNewTask:(task *)newTask{ 
    [self.tasks addObject:newTask]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.tasks count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    if(!cell){ 
     [tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(ListmaniaTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    task *task = [self.tasks objectAtIndex:indexPath.row]; 
    NSString *taskLabel = task.taskDescription; 
    cell.taskLabel.text = taskLabel; 
} 

@end 

回答

0

tableView:willDisplayCell:forRowAtIndexPath:方法具有代码应该是在tableView:cellForRowAtIndexPath:方法。就像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    if(!cell){ 
     [tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"ListmaniaCell" forIndexPath:indexPath]; 
    } 
    task *task = [self.tasks objectAtIndex:indexPath.row]; 
    NSString *taskLabel = task.taskDescription; 
    cell.taskLabel.text = taskLabel; 

    return cell; 
} 
+0

当我做到这一点不会改变任何东西(我刚试了一次,并没有改变) – crazyshark

1

viewDidLoad:

 [self.tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 

+0

,在启动应用程序崩溃和控制台说“终止于类型为NSException的未捕获异常“ – crazyshark

+0

@crazyshark我编辑了我的答案。 – dasdom

+0

崩溃时我使用self.tableView – crazyshark

0

我想出了问题。我在双倍链接的自定义单元中有一个出口。我也搬到线下到viewDidLoad中通过@dasdom的建议

[self.tableView registerNib:[UINib nibWithNibName:@"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListmaniaCell"]; 
相关问题