2012-04-20 33 views
0

私はビューが1つだけテーブルビューをロードしていれば、それは正常に動作しています。 しかし、以下のメソッドを使用して両方のテーブルビューをロードしようとしているときは、例外を以下に示します。テーブルビューのセルが2つのテーブルビューをロードすることができません

キャッチされない例外 'NSRangeException'、理由: '*- [NSArrayのobjectAtIndex:]:境界を超えてインデックス2 [0 .. 1]'

- (void)viewDidLoad { 
[super viewDidLoad]; 

array1 = [[NSArray alloc]initWithObjects:@"Start",@"End",@"Frequency",@"Time of Day",nil]; 
array2 =[[NSArray alloc]initWithObjects:@"Alarm",@"Tone",nil]; 

table1.scrollEnabled =NO; 
table2.scrollEnabled =NO; 

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
if (tableView == table1) ; 
    return 1; 

if (tableView == table2); 
    return 1; 

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if (tableView == self.table1) ; 
    return [array1 count]; 
if (tableView == self.table2) ; 
    return [array2 count]; 

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell... 


if (tableView == self.table1){ 
    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];  

} 
if (tableView == self.table2){ 
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];  

} 
return cell;} 

答えて

1

おそらく、あなたの配列の1つより大きなものにアイテムを持っているオブジェクトを要求します。 – numberOfSectionsInTableView:– tableView:numberOfRowsInSection:メソッドを呼び出して、どのテーブルが呼び出されているかを確認し、データ配列に応じて適切な値を返しましたか?
UPDATE
編集方法この方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (tableView == self.table1) 
     return 1; 

    if (tableView == self.table2) 
     return 1; 

    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == self.table1) 
     return [array1 count]; 
    if (tableView == self.table2) 
     return [array2 count]; 

    return 0; 
} 
関連する問題