数组消失
我所在的视图有 UIPickerView。这是我制作排序数组的地方。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray *studentNames = [[NSMutableArray alloc] init];
for (Student *student in course.students)
[studentNames addObject:student.name];
sortedArray = [studentNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[picker selectRow:(kRowMultiplier*sortedArray.count)/2 inComponent:0 animated:NO];
}
我可以在这两种方法中执行 NSLog([sortedArray ComponentsJoinedByString:@", "]);
并且它有效:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
但是
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
当我在此方法中执行相同的跟踪时它不起作用(它崩溃了):
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
我不明白为什么sortedArray除了在这一种方法之外在任何地方都可以工作。
I'm in a view that has a UIPickerView. Here is where I'm making my sortedArray.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray *studentNames = [[NSMutableArray alloc] init];
for (Student *student in course.students)
[studentNames addObject:student.name];
sortedArray = [studentNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[picker selectRow:(kRowMultiplier*sortedArray.count)/2 inComponent:0 animated:NO];
}
I can do an NSLog([sortedArray componentsJoinedByString:@", "]);
in these two methods and it works:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
and
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
but when I do that same trace in this method it doesn't work (It crashes):
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
I don't understand why the sortedArray works everywhere but in this one method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
sortedArrayUsingSelector:
的文档:也许原来的琴弦已经发布了?
顺便说一句,我发现你没有释放
studentNames
- 内存泄漏......From documentation of
sortedArrayUsingSelector:
:Maybe the original strings are already released?
BTW, I see that you don't release the
studentNames
- memory leak...