iOS为什么我的UIPickerview方法循环?

rat*_*p99 5 loops core-data uipickerview ios

我构建了我认为是一个简单的程序来探索CoreData关系的问题.

但是,这两种方法在segue之后意外循环(并成功到达目的地VC).涉及的方法是numberOfRowsInComponent(循环4次数)和titleForRow(循环5次数),NSLog在继续之前创建此输出:

2014-03-01 12:12:26.231 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.233 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.236 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.237 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.239 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>)
2014-03-01 12:12:26.242 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8c110> (entity: WMDGCategory; id: 0x8a8e2d0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p1> ; data: <fault>)
2014-03-01 12:12:26.246 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>)
2014-03-01 12:12:26.247 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8c110> (entity: WMDGCategory; id: 0x8a8e2d0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p1> ; data: <fault>)
2014-03-01 12:12:26.249 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>)
Run Code Online (Sandbox Code Playgroud)

这是两种方法的代码:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES];
    NSInteger *catCount;
    catCount = frc.fetchedObjects.count;
    NSLog(@"Number of categories is %zd", catCount);
    return frc.fetchedObjects.count;
}

#pragma mark Picker View delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    NSString * description;
    description = [[frc.fetchedObjects objectAtIndex:row]description];
    NSLog(@"Description of row is %@", description);
    return [[frc.fetchedObjects objectAtIndex:row]description];
}
Run Code Online (Sandbox Code Playgroud)

我很困惑.有人可以帮帮忙吗?

谢谢!

根据reecon的答案编辑如下

我移动了这一行:

frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES];
Run Code Online (Sandbox Code Playgroud)

进入viewDidLoad,但循环仍然存在.

Raf*_*fAl 4

您的数据源和委托方法可以被调用任意多次 - 这不是问题。

您应该假设方法:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
Run Code Online (Sandbox Code Playgroud)

或者

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
Run Code Online (Sandbox Code Playgroud)

可以随时调用,也可以多次调用。因此它的实现应该是轻量级的。将获取代码移至其他地方 - 例如移至viewDidLoad. 所以你只需要获取一次。然后,当选择器从其委托请求信息时,它不会导致执行不需要的提取。