我正在尝试使用 CoreData 中的存储数据填充我的 tableView。当 tableView 试图填充它的单元格时,单元格中字段的名称和日期都是空的。从 NSArray 创建的 NSMutableArray 的大小如下:
-(void)copyArrayToTableMutableArray:(NSArray *)coreDataArray
{
if(self.fetchedRecordsArray != nil)
{
modelArray = [NSMutableArray arrayWithArray:coreDataArray];
NSLog(@"%d", [modelArray count]);
}
}
显示例如有 3 个项目。当程序进入填充部分时,它会创建单元格,但它们是空的。这是用于填充的代码:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if([modelArray count] > 0)
{
Kwejki *tmpModel = [modelArray objectAtIndex:indexPath.row];
//Here it is empty NULL
NSLog(@"%@",[tmpModel name]);
cell.titleOfCell.text = [tmpModel name];
cell.dateOfAddedCell.text = [[tmpModel date] stringValue];
}
return cell;
}
我正在像这样将新项目保存到 CoreData:
-(void)addNewPosition:(ScrollViewViewController *)ScrollController recentlyDownloadedItem:(KwejkModel *)modelTmp
{
NSLog(@"DODAJE NOWA POZYCJE");
NSLog(@"%@",[modelTmp description]);
NSLog(@"%d", [modelArray count]);
//[self.tableView reloadData];
Kwejki * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Kwejki" inManagedObjectContext:self.managedObjectContext];
NSLog(@"%@", [modelTmp getNameOfItem]);
newEntry.name = [[NSString alloc] initWithString:[modelTmp getNameOfItem]];
newEntry.rating = [modelTmp getRateOfPosition];
newEntry.urlMain = [modelTmp getUrlAdress];
newEntry.contentUrl = [modelTmp getContentUrl];
newEntry.coverUrl = [modelTmp getCoverImage];
newEntry.date = [modelTmp getDateOfItem];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
else{
NSLog(@"UDALO SIE!!!");
}
[modelArray insertObject:newEntry atIndex:0];
[self.tableView reloadData];
}
我四处搜索但没有找到为什么它是空的。你知道为什么吗?
最佳答案
对于核心数据填充的 TableView ,您应该使用 NSFetchedResultsController
。然后您可以使用
Kwejki *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
不推荐以下内容:
如果您真的想坚持使用不明智的数组解决方案,那么确保您拥有属性或实例变量会有所帮助。显然,在 cellForRowAtIndexPath
中,您的数组已被释放。
@property (nonatomic, strong) NSMutableArray *modelArray;
关于ios - 核心数据返回空字符串和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24845799/