コアデータから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つのセクションがあります。2つの異なるカスタムセルを持つセクション0と、配列からのデータを表示するセクション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に変更すると、すべて正常に機能します...