iOS 为啥我的 UIPickerview 方法在循环?
Posted
技术标签:
【中文标题】iOS 为啥我的 UIPickerview 方法在循环?【英文标题】:iOS Why are my UIPickerview methods looping?iOS 为什么我的 UIPickerview 方法在循环? 【发布时间】:2014-03-01 19:30:54 【问题描述】:我构建了一个我认为很简单的程序来探索 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>)
以下是这两种方法的代码:
- (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];
我很困惑。有人可以帮忙吗?
谢谢!
根据以下 reecon 的回答编辑
我移动了这条线:
frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES];
进入viewDidLoad
,但循环仍然存在。
【问题讨论】:
【参考方案1】:您的数据源和委托方法可以被多次调用 - 这不是问题。
你应该假设方法:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
或
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
可以随时多次调用。因此,它的实现应该是轻量级的。将获取代码移至别处 - 例如到viewDidLoad
。所以你只取一次。 Then when the picker requests information from its delegate, it does not result in performing a not needed fetch.
【讨论】:
您的数据源和委托方法可以被调用任意次数——这不是问题。您的代码是否正常工作? 是的,到目前为止,一切都很好。我还有东西要实现,但我被那个小怪癖挂断了。非常感谢您让我直截了当,但如果您再次收到我的来信,请不要感到惊讶! :) 没问题,伙计:)我是来帮忙的。如果有帮助,您能接受并支持我的回答吗? :)以上是关于iOS 为啥我的 UIPickerview 方法在循环?的主要内容,如果未能解决你的问题,请参考以下文章
ios UiPickerView 与 CountryPicker 类
如何在iOS 7中更改UIPickerView中的文本字体?