Ios 在单个视图中加载2个表视图

Ios 在单个视图中加载2个表视图,ios,uitableview,loaddata,Ios,Uitableview,Loaddata,我在一个视图控制器中有两个表视图(表1、表2)。我有2个数据阵列(dataArray1、dataArray2)。 我想用相应的数据数组加载表视图,即(table1=dataArray1,table2=dataArray2)。但应用程序崩溃了?这个代码有什么问题?任何帮助都将不胜感激。请不要建议使用2个视图控制器 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

我在一个视图控制器中有两个表视图(表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;
}

为两个表视图提供2个不同的单元格标识符

因为两个表单元不同,它们应该被重用,为了正确地维护重用,唯一标识符需要分开

试试这个

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

    static NSString *CellIdentifier1 = @"Cell1";
    static NSString *CellIdentifier2 = @"Cell2";

    UITableViewCell *cell;

    if(tableView == self.table1)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:CellIdentifier1];
        }
        cell.textLabel.text =[dataArray1 objectAtIndex:indexPath.row];

    }
    if(tableView == self.table2)
    {
       cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:CellIdentifier2];
        }
        cell.textLabel.text =[dataArray2 objectAtIndex:indexPath.row];
    }
    return cell;
}

通常不会在一个视图中创建两个表视图。 而是创建一个分区。 即使如此,如果您仍然需要两个表视图,最好使用view类并为它们创建单独的委托。 我有一个密码 这显示了视图与表视图的结合使用。
希望对您有所帮助。

哪一行正在崩溃,请发布崩溃时出现的错误。如果没有,请尝试其他操作。如果没有,请添加您收到的错误消息。。。(确保设置了异常断点)我收到以下错误。***由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引4超出边界[0..3]”***第一次抛出调用堆栈:该问题已得到解决。我使用的不是表名,而是标记。现在开始工作了。谢谢(NSInteger)tableView:(UITableView*)tableView numberofrowsinssection:(NSInteger)section{NSInteger number;if(tableView.tag==1)number=[data1 count];if(tableView.tag==2)number=[data2 count];return number;}该链接未打开。@iOS:它正在我的机器上工作。如果是这样的话,只需在谷歌上搜索BGRaioList,然后打开github页面