【问题标题】:load 2 tableviews in a single view iOS [closed]在单个视图iOS中加载2个表视图[关闭]
【发布时间】: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


【解决方案1】:

通常您不会在一个视图中创建两个表视图。 而是创建一个部分。 即使那样,如果您仍然需要两个表格视图,最好使用 View Class 并为它们制作单独的代表。 我有一个代码 https://github.com/bishalg/BGRadioList 它向您展示了视图与表格视图的使用。 希望对你有帮助。

【讨论】:

  • 那个链接没有打开。
  • @iOS :它在我的机器上工作。如果是这种情况,只需在 google 上搜索 BGRaioList,然后打开 github 页面 github.com/bishalg?tab=repositories
【解决方案2】:

为两个表视图提供 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;
}

【讨论】:

    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多