使用核心数据对象更改文本对象

时间:2014-01-10 01:19:13

标签: ios core-data nsfetchedresultscontroller

我需要替换tableView中的行内容。在这种情况下,行从交换机循环填充。我想用核心数据实体中的对象更改文本对象。我向您展示了两个牵连的方法:

//cellForRowAtIndexPath    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }

        // Configure the cell.

        switch (indexPath.row)
        {
            case 0 : cell.textLabel.text = @"First Cell"; break;
            case 1 : cell.textLabel.text = @"Second Cell"; break;
            case 2 : cell.textLabel.text = @"Third Cell"; break;
            case 3 : cell.textLabel.text = @"Fourth Cell"; break;
            case 4 : cell.textLabel.text = @"Fifth Cell"; break;
            case 5 : cell.textLabel.text = @"Sixth Cell"; break;
            case 6 : cell.textLabel.text = @"Seventh Cell"; break;
            case 7 : cell.textLabel.text = @"Eighth Cell"; break;
            default : cell.textLabel.text = [NSString stringWithFormat:@"Cell %i",indexPath.row + 1];
        }

        //cell.detailTextLabel.text = ...;

        return cell;
    }

并且

//NSFetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"ToDoItems" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"tdText" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
                                                   cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

1 个答案:

答案 0 :(得分:0)

如果您使用 Master-Detail Application 模板在Xcode中创建一个新项目,然后选中下一个对话框中的使用核心数据复选框,您将获得所有你需要的样板代码。例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[object valueForKey:@"timeStamp"] description];
}

PS。我认为最好将您的实体命名为“ToDoItem”而不是“ToDoItems”,在这种情况下不要使用复数