Tableview崩溃

时间:2012-05-27 16:22:56

标签: iphone xcode arrays

我正在从核心数据中检索数据到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个部分 - 第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]'

但是数组不是emtpy

2012-05-27 18:10:39.010 TabWithCore[7249:fb03] Array 1 is (
1234567
) and Array 2 is (
Host
)
奇怪的是,当我从2改变第0部分的值numberofrows时,>到1,一切正常......

3 个答案:

答案 0 :(得分:0)

你的数组中只有一个对象...而你正试图从中设置两个单元格。因此,当它试图访问数组的第二个对象时,它会崩溃,因为第二个对象不在那里......

答案 1 :(得分:0)

您遇到的主要问题是您尚未初始化temp数组。您刚刚通过名称和赋值来声明它。 NSArray *temp = [context executeFetchRequest:fetchRequest error:&error];

因此,此时,您的temp数组未填充并且结果,您的hostArray也未填充。 就我的观点而言,出于这个原因,您会收到此错误Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

请尝试使用alloc init temp数组的此解决方案...希望它可以解决您的问题... :)

答案 2 :(得分:0)

执行下面的代码而不检查你所在的部分。所以当section为0(2行)且indexPath.row为1时,这会在尝试访问hostArray [1]时引发异常,而它有只有1个值。

if ([hostArray count] != 0) {
    [defaultCellLabel setText:[hostArray objectAtIndex:indexPath.row]];
}

要解决此问题,请在执行任何此代码之前检查该部分。以下内容将起作用。

if (indexPath.section == 1)    
{
    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]];
    }
    return cellC;
}
相关问题