0

我有一个视图控制器,显示来自核心数据实体的项目。我还有一个表格视图列出了来自同一实体的记录。该表是可编辑的,用户可以删除所有记录。发生这种情况时,持有pickerview 的视图控制器会爆炸,因为它正在寻找空数组中的记录。如何防止这种情况?我假设我需要在 objectAtIndex:row 做一些不同的事情......

# pragma mark PickerView Section

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;  // returns the number of columns to display.
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [profiles count];  // returns the number of rows
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // Display the profiles we've fetched on the picker
    Profiles *prof = [profiles objectAtIndex:row];
    return prof.profilename;

}

//If the user chooses from the pickerview

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    selectedProfile = [[profiles objectAtIndex:row]valueForKey:@"profilename"];
}

编辑* *

好的,我找到了导致问题的原因...在 PickerView 上我设置了默认值..

self.profiles = [[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

selectedProfile = [[profiles objectAtIndex:0]valueForKey:@"profilename"];

[pickerView reloadAllComponents];

当我把它拿出来时,它就像一个冠军。

4

0 回答 0