简体   繁体   English

TableViewCell中的CoreData相关数据

[英]CoreData Related data in a tableviewcell

I am working on a project with related entities in CoreData. 我正在与CoreData中的相关实体进行项目。 I want to have a tableview with cells that show attributes of records from a related entity, but am struggling to find a good way to do this. 我想要一个带有单元格的表格视图,该单元格显示来自相关实体的记录的属性,但是正在努力寻找一种实现此目的的好方法。

Here's the basics: 基本知识如下:

My custom TableViewCell has five things: 我的自定义TableViewCell有五件事:

UILabel *eventTime
UILabel *person1Name
UIImageView *person1Image //storing a string
UILabel *person2Name
UIImageView *person2Image //storing a string

Event entity has a time, person1Name, and person2Name attributes and a to-many relationship to my Person entity. 事件实体具有时间,person1Name和person2Name属性,并且与我的Person实体具有多对多关系。

Person entity has string attributes for name and image and a to-many relationship to my Events entity. Person实体具有用于名称和图像的字符串属性,并且与我的Events实体具有多对多关系。

Events will always have two related person records. 事件将始终具有两个相关的人记录。

Persons will be affiliated with many events. 人们将参与许多活动。

In my cellForRowAtIndexPath, how do I configure the cell so that the related person attributes show for each event? 在我的cellForRowAtIndexPath中,如何配置该单元,以便为每个事件显示相关人员属性?

I was playing with this: 我在玩这个:

EventsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EventCell" forIndexPath:indexPath];
Event *event = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSSet *persons = event.eventPersons;  //eventPersons is the NSSet in the NSManagedObject subclass…

// Configure the cell...

for (Player* p in persons) {
    if (p.firstName == event.person1Name) {
        cell.player1NameLabel.text = p.firstName;
        cell.player1ImageView.image = [UIImage imageWithContentsOfFile:p.playerImage];
    } else if (p.firstName == event.person2Name) {
        cell.player2NameLabel.text = p.firstName;
        cell.player2ImageView.image = [UIImage imageWithContentsOfFile:p.playerImage];
    }
}

But I may have multiple people with the same first name, so this fails to work properly in all cases. 但是我可能有多个名字相同的人,因此在所有情况下都无法正常工作。 I don't want to store a bunch of duplicate information in the Event entity for comparing records and would prefer a more elegant way to grab the attributes of the two related person records in each cell. 我不想在Event实体中存储一堆重复的信息来比较记录,而是希望以更优雅的方式在每个单元格中获取两个相关人员记录的属性。

Thanks. 谢谢。

You already have duplicate information in the storage of the names. 名称存储中已经有重复的信息。 But your problem is that they aren't unique. 但是您的问题是它们不是唯一的。

So, change them to be unique (Ie Use a different identifier than the name) or use a different method of recording first and second (Ie Use an ordered relationship). 因此,将它们更改为唯一(即,使用与名称不同的标识符)或使用不同的记录第一和第二种方法(即,使用有序关系)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM