2015-08-28 90 views
0

我有以下代码在UITableView中创建我的单元格。UITableView的第一个单元格为空(UIView中的TableView)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

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

    if (nil == cell) { 
     [tableView registerNib:[UINib nibWithNibName:@"FutureAppointmentsViewCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; 
     cell = [[FutureAppointmentsViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 
    } 

    NSUInteger position = indexPath.row; 
    cell.appointmentDescription.text = [NSString stringWithFormat:@"%lu. %@. %@", (unsigned long)position, @"Steve", @"10/03/"]; 

    return cell; 
} 

问题是,tableView的第一个单元格丢失。它应该从0.史蒂夫开始,而是开始1.史蒂夫。也只有4个列表中的元素,而不是5.

enter image description here

当我放置一个断点中的代码,所述第一小区是零。

有谁知道可能发生了什么?

+0

也许你的_header_视图涵盖了它?你有没有试图拉下tableview来查看单元格是否在_something_下,也许呢? – holex

回答

3

,就把这行代码:

[tableView registerNib:[UINib nibWithNibName:@"FutureAppointmentsViewCell" 
             bundle:nil] 
forCellReuseIdentifier:CellIdentifier]; 

viewDidLoad。它不属于cellForRowAtIndexPath。一旦你这样做,那么你不再需要检查单元格是否为零。

变化的cellForRowAtIndexPath:

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


    NSUInteger position = indexPath.row; 
    cell.appointmentDescription.text = 
    [NSString stringWithFormat:@"%lu. %@. %@", 
    (unsigned long)position, @"Steve", @"10/03/"]; 

    return cell; 
} 
+0

对不起,我忘了在我的问题中提到我正在处理的代码已经完成了视图,所以我没有viewDidLoadMethod来处理。 – pls

+0

然后把它放在视图的init方法中。将其移出cellForRowAtIndexPath。顺便说一句,你真的应该使用ViewControllers。 –

+0

我会试试看。我继承了这个代码库,所以没有选择。你不需要说服我使用视图控制器:-)。 – pls

1

我觉得我有类似的问题,我是用UITableViewUIView当在init方法初始化它。

我无法找到该行为的很好解释,但我发现了一个棘手的解决方案 - 我正在从UIViewControllerviewDidAppear方法中重新加载UITableView实例。

我也想知道,为什么UITableView没有画全部UITableViewCell

相关问题