我正在将核心数据中的数据检索到 2 个数组中。
-(void)loadData{
AppDelegate *delegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityLocation = [NSEntityDescription entityForName:@"Devices" inManagedObjectContext:context];
fetchRequest.entity = entityLocation;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"location = %@",actuallLocationName];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *temp = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"matches in hostArray = %d",[temp count]);
hostArray = [temp valueForKey:@"hostname"];
deviceArray = [temp valueForKey:@"devicename"];
[myTable reloadData];
}
在我的 UITableView 中,我有 2 个部分 --section 0 有 2 个不同的自定义单元,第 1 部分显示来自数组的数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 2;
}
return [hostArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCell = @"CustomCell";
static NSString *CustomCell1 = @"CustomCell1";
static NSString *CustomCell2 = @"CusrtomCell2";
UITableViewCell *cellA = [tableView dequeueReusableCellWithIdentifier:CustomCell1];
if (cellA == nil) {
[[NSBundle mainBundle] loadNibNamed:@"SettingCellHost" owner:self options:nil];
cellA=hostNameCell;
self.hostNameCell=nil;
}
UITableViewCell *cellB = [tableView dequeueReusableCellWithIdentifier:CustomCell2];
if (cellB == nil) {
[[NSBundle mainBundle] loadNibNamed:@"SettingCellDevice" owner:self options:nil];
cellB=deviceNameCell;
self.deviceNameCell=nil;
}
UITableViewCell *cellC = [tableView dequeueReusableCellWithIdentifier:CustomCell];
if (cellC == nil) {
[[NSBundle mainBundle] loadNibNamed:@"DefaultCell" owner:self options:nil];
cellC=defaultCell;
self.defaultCell=nil;
}
if ([hostArray count] != 0) {
[defaultCellLabel setText:[hostArray objectAtIndex:indexPath.row]];
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
return cellA;
}
}
if (indexPath.section == 0) {
if (indexPath.row == 1) {
return cellB;
}
}
return cellC;
}
启动应用程序崩溃后
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
但数组不是空的
2012-05-27 18:10:39.010 TabWithCore[7249:fb03] Array 1 is (
1234567
) and Array 2 is (
Host
)
奇怪的是,当我将第 0 部分的值 numberofrows 从 2 --> 更改为 1 时,一切正常......