【问题标题】:tableView show title use core data (relation)tableView 显示标题使用核心数据(关系)
【发布时间】:2012-12-11 14:50:33
【问题描述】:

我有 2 个实体(和两个 tableViews)我的第一个实体名称是 Person,第二个是 Event,在我的第一个 tableView 中有人的名字,第二个是他们的事件,我在 Person 和 Event 之间建立了一对多的关系。我只希望当用户点击其中一个名称时,第二个 tableView 将向他显示该人的事件(类型)。问题是我得到了空单元格。

这就是我添加新事件的方式:

 - (void) addEventControllerDidSave{


        NSManagedObjectContext* context = [self managedObjectContext];
        Event *newEvent = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
        [newEvent setType:@"dennis"];
        [currentPerson addEventsObject:newEvent];

        NSError *error;
        if (![[self managedObjectContext] save:&error])
        {
            NSLog(@"Problem saving: %@", [error localizedDescription]);
        }

        [self dismissViewControllerAnimated:YES completion:nil];
    }

我知道这个方法不是动态的,我总是会得到事件类型“dennis”,这只是为了测试。

我的第二个 tableView 方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[self currentPerson] events] count];
}

最重要的方法我认为问题出在这里(或者在save方法中):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"eventsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (nil == cell)
    {
        NSEnumerator *enumerator = [[[self currentPerson] events] objectEnumerator];
        Event *event = [[enumerator nextObject]objectAtIndexPath:indexPath];
        [[cell textLabel]setText:[event type]];
    }

    return cell;
}

更新: 正如ophychius所说,我将我定义的细胞系从if中移出并将它们更改为:

//create cell use relation
    NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects];
    Event *newEvent = [eventsArray objectAtIndex:indexPath.row];
    [[cell textLabel]setText: [newEvent type]];

【问题讨论】:

    标签: ios core-data tableview cell relation


    【解决方案1】:

    尝试移动线条

        NSEnumerator *enumerator = [[[self currentPerson] events] objectEnumerator];
        Event *event = [[enumerator nextObject]objectAtIndexPath:indexPath];
        [[cell textLabel]setText:[event type]];
    

    在 if 块之外。如果您还没有一个单元格,则在 if 内部需要创建单元格的代码(您尝试在 if 块之前检索)

    现在您可能没有创建单元格,如果您创建了单元格,则不会在其中放入文本,因为一旦您有了一个单元格,就会跳过 if 块。

    这是一个例子:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        NSEnumerator *enumerator = [[[self currentPerson] events] objectEnumerator];
        Event *event = [[enumerator nextObject]objectAtIndexPath:indexPath];
        [[cell textLabel]setText:[event type]];
    
        return cell;
    }
    

    【讨论】:

    • ophychius 的解决方案是正确的,但不确定为什么要从那里开始使用此枚举器代码。只需获取 [[[self currentPerson] events] objectAtIndex:indexPath.row] type];因为如果我们得到一个事件列表,代码看起来不正确。
    • "no visible interface for 'NSSet' 声明选择器 'objectAtIndex:' 事件是 NSSet,这就是我尝试使用枚举器的原因。当我尝试使用 @ophychuis 解决方案时,我的应用程序崩溃了,并且出现此错误:reason: '-[Event objectAtIndexPath:]: unrecognized selector sent to instance
    • 我终于解决了这个问题: NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects];事件 *newEvent = [eventsArray objectAtIndex:indexPath.row]; [[cell textLabel]setText: [newEvent type]];我将ophychius标记为正确答案。因为他的回答非常正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多