【发布时间】:2014-07-19 23:07:41
【问题描述】:
我正在尝试使用 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];
}
我已经四处搜索,但没有找到为什么它是空的。你知道为什么吗?
【问题讨论】:
-
你能粘贴
modelArray = [NSMutableArray arrayWithArray:coreDataArray]; NSLog(@"%d", [modelArray count]);的输出吗 -
您的
newEntry属性是否在addNewPosition中正确填充?您是否尝试在设置后记录它们? -
是的,我已经记录了它们并设置了属性。 @codester NSLog 显示项目数 = 5。总是比以前多一个。
-
tmpModel 不是
nil? -
tmpModel 不为空
标签: ios objective-c uitableview core-data