【发布时间】:2013-09-06 10:25:52
【问题描述】:
我在视图控制器中有 2 个表视图(表 1、表 2)。我有 2 个数据数组(dataArray1,dataArray2)。 我想用相应的数据数组加载表视图,即(table1 = dataArray1,table2 = dataArray2)。我正在尝试下面的代码。但是应用程序崩溃了?这段代码有什么问题?任何帮助,将不胜感激。请不要建议使用 2 个视图控制器。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.table1)
return [dataArray1 count];
if(tableView == self.table2)
return [dataArray2 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];
}
if(tableView == self.table1)
{
cell.textLabel.text =[dataArray1 objectAtIndex:indexPath.row];
}
if(tableView == self.table2)
{
cell.textLabel.text =[dataArray2 objectAtIndex:indexPath.row];
}
return cell;
}
【问题讨论】:
-
它在哪一行崩溃,请发布您在崩溃时遇到的错误。
-
如果条件不满足则尝试 Else
-
请添加您收到的错误消息...(确保您设置了异常断点)
-
我收到以下错误。 *** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]”*** 第一次抛出调用堆栈:该问题得到解决。我没有使用表名,而是使用标签。它现在工作。谢谢。 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger number; if(tableView.tag == 1) number = [data1 count]; if(tableView.tag == 2) number = [data2 count];返回号码; }
标签: ios uitableview loaddata