2014-07-19 31 views
0

我試圖用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]; 
} 

我搜索周圍,但還沒有成立,爲什麼它是空的。你知道爲什麼嗎?

+0

可以粘貼'modelArray = [NSMutableArray arrayWithArray:coreDataArray];的輸出嗎? NSLog(@「%d」,[modelArray count]);' – codester

+1

你的'newEntry'屬性是否在'addNewPosition'中正確填充?您是否嘗試過在設置後記錄它們? – spassas

+0

是的,我已經記錄他們,屬性設置。 @codester NSLog顯示的項目數= 5。總是比以前多一個。 – DKM

回答

1

對於Core Data填充的表格視圖,您應該使用NSFetchedResultsController。然後,你可以用

Kwejki *item = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

下不建議可靠地檢索您的對象:
如果你真的想堅持自己的不明智陣列的解決方案,這將有助於確保你有一個屬性或實例變量。顯然,在cellForRowAtIndexPath你的數組已經被釋放。

@property (nonatomic, strong) NSMutableArray *modelArray; 
+0

我在這個類中有變量NSMutable數組,現在我已經改變它爲您的提案,它也沒有幫助。當我保存項目時,它有所有的字符串等,但當我試圖再次加載它們時,它們是空的。 – DKM

+0

但是,在代碼中執行fetch之後,我得到5個項目,但沒有字符串。哪裏犯了一個錯誤? – DKM

+0

爲什麼你不想使用'NSFetchedResultsController'? – Mundi

1

它看起來不像你正在使用UIManagedDocuments,所以你可能需要在填充newentry字段後添加一個[self.managedObjectContext保存]添加新的位置。我知道你不應該這樣做,即數據在內存中,但是如果你在插入和提取中使用不同的上下文,那麼在完成保存之前你不會看到數據。它可能沒有幫助,但試試看吧。