2012-05-27 83 views
0

我正在將核心數據中的數據檢索到2個數組中。Tableview crash

-(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個不同的customcells,而第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 
) 

奇怪的是,當我將0的值numberofrows從2 - >更改爲1時,所有工作都很好...

+0

這裏我發佈了我的答案。希望它可以幫助你。 – Krunal

回答

0

以下一段代碼在沒有檢查你所在的部分的情況下執行。所以當section爲0(2 rows)並且indexPath.row爲1時,這會在嘗試訪問hostArray [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; 
} 
+0

thx chirag,多數民衆贊成它!花這麼多小時來解決這個問題,這很簡單:-) – HugoBoss

0

您的數組只有一個對象在其中...並且您正試圖設置兩個細胞。所以當它試圖訪問數組的第二個對象時,它會崩潰,因爲第二個對象不存在...

+0

我想在第1節[數組計數]中顯示2個不同的自定義單元格的數組0。部分0訪問數組也是如此? – HugoBoss

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 inittemp陣列的這種解決方案...希望可以解決您的問題... :)