2010-07-14 69 views
0

我在具有UIPickerView的视图中。这是我制作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]; 
} 

我可以做这两种方法的NSLog([sortedArray componentsJoinedByString:@", "]);,它工作

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 

但是当我做同样的痕迹在这个方法中它没有按't work(It crash):

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

我不明白为什么sortedArray无处不在,但在这一个方法。

+0

哪里定义数组排序? – 2010-07-14 21:42:27

+0

sortedArray在标题中定义。 – tazboy 2010-07-14 22:32:09

+0

你试过 [self setSortedArray:[studentNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare :)]]; – 2010-07-15 01:13:46

回答

0

sortedArrayUsingSelector:文件:

新的数组包含接收器的元素,而不是它们的副本的引用。

也许原始字符串已经发布?

BTW,我看你不松开studentNames - 内存泄漏......

+0

我不确定我在哪里搞砸了,但因为你是唯一给出答案的人,我会给你信用。因为你注意到我的泄漏。谢谢。 – tazboy 2011-05-15 22:44:08