当滚动时,UITableView dataSource对象变为空

问题描述:

我是CoreData中的新成员,并且使用MagicalRecord来进行排序。我的问题是,我有UITableViewNSArray作为dataSource填充从CoreData分贝取自对象,并且一切似乎都很好,直到我滚动表一段时间。当滚动时,UITableView dataSource对象变为空

这里是我的代码:

用于读取数据(MyDatabase.m)方法:

+(NSArray *)getEntities{ 
    ... 

return [MyEntity MR_findAllSortedBy:@"name" ascending:YES withPredicate:predicate]; 
} 

这里是我取,在我的ViewController设置数据UITableView

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    myEntitiesArray = [MyDatabase getEntities]; 

    if(myEntitiesArray.count != 0) 
     [myTableView setTableData:myEntitiesArray]; 
} 

这是setTableData方法im plementation在MyTableView.m:

- (void)setTableData:(NSArray *)array { 
    if (array && [array count] > 0) { 
     _tableData = array; 
     [self reloadData]; 
    } 
} 

这里是我如何设置我的细胞MyTableView.m:

- (void)tableView:(UITableView *)tableView willDisplayCell:(SSCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.nameLabel.text = [(MyEntity *)_tableData[indexPath.row] name]; 

} 

我试图把一个NSLog(@"name is %@",[(MyEntity *)_tableData[indexPath.row] name])willDisplayCell,发现当细胞变为空,NSLog打印出消息“名称是(空)”。我知道这个问题很可能在很多人遇到这个问题之前就解决了很多次。希望有人能帮助我解决这个问题太:)

UPDATE:cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"ssCell"; 
    SSCell *cell = (SSCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    if(!cell) { 
     [self registerNib:[UINib nibWithNibName:@"SSCell" bundle:nil] forCellReuseIdentifier:cellIdentifier]; 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
     [cell setSelectedBackgroundView:selectedBackgroundView]; 
} 
    cell.nameLabel.text = [(MyEntity *)_tableData[indexPath.row] name]; 
    return cell; 
} 

我也呼吁里面MyTableView.m init方法这个方法:

[self registerNib:[UINib nibWithNibName:@"SSCell" bundle:nil] forCellReuseIdentifier:@"ssCell"]; 
+0

其中是您的cellForRowAtIndexPath方法?我想你使用willDisplayCell时,你必须使用cellForRowAtIndexPath –

+0

@CCastro看到我的更新,我把行从'willDisplayCell'移动到'cellForRowAtIndexPath'并没有运气 – vendettacore

+0

更改此单元= [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];对于这个单元格= [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; –

你应该通过调用init初始化单元。相反,你正在做以下几点:

如果(细胞!){ [自registerNib:[UINib nibWithNibName:@ “SSCell” 捆绑:无] forCellReuseIdentifier:cellIdentifier]。 cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; [cell setSelectedBackgroundView:selectedBackgroundView];

第二次调用在没有可用的单元时尝试再次使用现有单元。那可能会再次返回零。

要非常小心目标C的“特征”,其中调用无对象的方法什么也不做。它不像使用Java那样使用null.pointer.exception崩溃,它可能会漂浮在[cell setSelectedBackgroundView:selectedBackgroundView]和其他所有行上,而没有任何问题。

+0

我知道'UITableView'不会为每行创建一个'UITableViewCell'因为重用。我创建我的单元格并在'cellForRowAtIndexPath'中重用它,单元格本身没有问题。数组内的对象存在问题。附:试图移动到'cellForRowAtIndexPath'行,没有运气:( – vendettacore

您必须使用cellForRowAtIndexPath。在这种方法中,单元被分配。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    /* 
    * This is an important bit, it asks the table view if it has any available cells 
    * already created which it is not using (if they are offscreen), so that it can 
    * reuse them (saving the time of alloc/init/load from xib a new cell). 
    * The identifier is there to differentiate between different types of cells 
    * (you can display different types of cells in the same table view) 
    */ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    /* 
    * If the cell is nil it means no cell was available for reuse and that we should 
    * create a new one. 
    */ 

    if (cell == nil) { 

     /* 
     * Actually create a new cell (with an identifier so that it can be dequeued). 
     */ 

      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    /* 
    * Now that we have a cell we can configure it to display the data corresponding to 
    * this row/section 
    */ 

     cell.nameLabel.text = [(MyEntity *)_tableData[indexPath.row] name]; 
    return cell; 
}