我試圖用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]);' – codester
你的'newEntry'屬性是否在'addNewPosition'中正確填充?您是否嘗試過在設置後記錄它們? – spassas
是的,我已經記錄他們,屬性設置。 @codester NSLog顯示的項目數= 5。總是比以前多一個。 – DKM