tableview cell无法加载两个tableviews
问题描述:
我必须在我的视图中的两个表视图如果我加载只有一个表视图它工作正常。 但是当我尝试使用下面的方法加载两个表视图时,它会给出以下例外。tableview cell无法加载两个tableviews
未捕获的异常 '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;}
答
您可能要求索引大于某个数组的某个对象的项目。你是否正确地执行了– 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;
}