我有一个viewcontroller显示来自核心数据实体的项目。我还有一个tableview列出来自同一实体的记录。该表是可编辑的,用户可以删除所有记录。发生这种情况时,视图控制器持有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];
当我把它拿出来时,它就像一个冠军。