ios - 核心数据返回空字符串和内容

标签 ios objective-c uitableview core-data

我正在尝试使用 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/

相关文章:

ios - 在 UITableView 中处理 UIButton 照片网格的最有效方法是什么?

javascript - 在 JavaScript 中检测页面是否加载到 WKWebView 中

ios - 在应用程序购买中 - 应用程序有时会崩溃

objective-c - 在 Safari 中连接 Wifi 后返回应用程序

iphone - 纹理 ID 未使用相同的 ID

ios - 网络连接应仅在更改时出现

ios - 单击多个单元格上的 UItableview 正在快速检查 3

ios - 添加约束时 TableView 消失

ios - 如何从 TableView 的顶部删除顶部空间?

ios - UITableView 单元格嵌入 View Controller 自动调整大小?