Я новичок в CoreData
и с помощью MagicalRecord
, чтобы управлять им. Моя проблема в том, что у меня есть UITableView
с NSArray
как dataSource, заполненный объектами, которые извлекаются из CoreData
db, и все кажется прекрасным, пока я не прокручу таблицу несколько раз.UITableView dataSource объекты становятся нулевыми при прокрутке
Вот мой код:
метод для извлечения данных (MyDatabase.m):
+(NSArray *)getEntities{
...
return [MyEntity MR_findAllSortedBy:@"name" ascending:YES withPredicate:predicate];
}
Вот как я принести и установить данные UITableView
в моем ViewController:
- (void)viewDidLoad {
[super viewDidLoad];
myEntitiesArray = [MyDatabase getEntities];
if(myEntitiesArray.count != 0)
[myTableView setTableData:myEntitiesArray];
}
Адрес setTableData
Метод im зация в 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
печатает сообщения «имя (null)». Я знаю, что этот вопрос, возможно, решался многими людьми и много раз, прежде чем я столкнулся с этой проблемой. Надеюсь, что кто-то поможет мне решить это тоже :)
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"];
где ваш метод cellForRowAtIndexPath?Я думаю, что вы используете willDisplayCell, когда вам нужно использовать cellForRowAtIndexPath –
@CCastro см. Мое обновление, я переместил строку с 'willDisplayCell' в' cellForRowAtIndexPath' и не повезло – vendettacore
Измените эту ячейку = [tableView dequeueReusableCellWithIdentifier: cellIdentifier forIndexPath: indexPath]; для этой ячейки = [tableView dequeueReusableCellWithIdentifier: cellIdentifier]; –