Ios 核心数据一对多关系获取数据
我使用一对多关系将JSON数据存储到CoreData。我可以使用NSFetchRequest和fast enumeration取回数据,但数据的顺序格式不是我需要的,并且无法在我的UITableViewCells中使用。我如何才能做到这一点 这是我的密码 这是我的数据模型 我把这些代码都放在viewdiload中Ios 核心数据一对多关系获取数据,ios,objective-c,core-data,Ios,Objective C,Core Data,我使用一对多关系将JSON数据存储到CoreData。我可以使用NSFetchRequest和fast enumeration取回数据,但数据的顺序格式不是我需要的,并且无法在我的UITableViewCells中使用。我如何才能做到这一点 这是我的密码 这是我的数据模型 我把这些代码都放在viewdiload中 这里我从NSLog获取数据,但我的问题是我能够打印数据,但无法将数据传递到表视图您发布的JSON片段显示(与我在评论中的假设相反,抱歉)每个交易都可以有许多交易部分 您的DealSe
这里我从NSLog获取数据,但我的问题是我能够打印数据,但无法将数据传递到表视图您发布的JSON片段显示(与我在评论中的假设相反,抱歉)每个
交易
都可以有许多交易部分
您的DealSection
实体有一个名为alldeals
的关系,该关系当前为一对一关系。也就是说,每个DealSection
只能有一个Deal
。我想这对很多人来说应该是个好消息。举个例子,您发布的JSON中的交易有ID=6(name=“Services”)和ID=8(name=“Wellness”)的部分。假设您与ID=6的节有另一笔交易-您想使用建立与现有DealSection
的关系,还是创建新的DealSection
目前,您的代码创建了一个新的DealSection
,但我认为您可能希望与现有的DealSection
建立关系。要让它起作用,你需要
a) 修改您的数据模型,使关系更加密切
b) 修改存储多人关系数据的代码,使其从尝试获取具有正确ID的DealSection
开始。如果找到一个ID,请将该DealSection
添加到交易的sectionRelation
。如果未找到,则创建一个新的DealSection
,并将其添加到Deal
编辑
要在tableView中显示数据,需要实现三种数据源方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// A table view section for each DealSection:
return [self.fetchData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
DealSection *dealSection = self.fetchData[section];
NSArray *deals = [dealSection.alldeals allObjects];
return [deals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Amend this identifier to match your cell prototype
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Set up the cell...
DealSection *dealSection = (DealSection *)self.fetchData[indexPath.section];
NSArray *deals = [dealSection.alldeals allObjects];
Deal *deal = (Deal *)deals[indexPath.row];
cell.textLabel.text = deal.nameAttribute;
return cell;
}
这是基本的表视图内容,因此,如果有任何不清楚的地方,我推荐我在评论中提到的教程。是否有其他方法获取数据我认为您可能定义了错误的关系。它看起来像是一笔交易。sectionRelation是“对多”,不是“对一”吗?与之相反,
DealSection.alldeals
是“对一”,不是应该是“对多”吗?但deal不是唯一的一种类型,我的意思是不可能是一个deal属于多个部分,因为我的JSON响应有一些部分,如数组和对象Dining、travel。。。。。就像这样,我认为要处理很多关系您的数据模型的屏幕截图意味着一笔交易有很多部分,而每个部分只有一笔交易。这就是你想要的吗?这意味着我必须使用多对多关系?这意味着DealDealSection是我需要修改数据模型,但为什么我们需要这个方法-(void)addAlldealsObject:(Deal*)value;-(void)removeAlldealsObject:(交易*)值;-(void)addAlldeals:(NSSet*)值;-(void)removeAlldeals:(NSSet*)值;这是DealSection实体中的数据,我将数据存储到DealSection的方式是正确的,或者我在这方面有任何错误否分区将只有唯一的ID(ID不会重复,正如您所说的示例ID=6)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// A table view section for each DealSection:
return [self.fetchData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
DealSection *dealSection = self.fetchData[section];
NSArray *deals = [dealSection.alldeals allObjects];
return [deals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Amend this identifier to match your cell prototype
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Set up the cell...
DealSection *dealSection = (DealSection *)self.fetchData[indexPath.section];
NSArray *deals = [dealSection.alldeals allObjects];
Deal *deal = (Deal *)deals[indexPath.row];
cell.textLabel.text = deal.nameAttribute;
return cell;
}