0

我正在使用 Detail Right 样式的单元格(原型单元格)创建表格视图。在数组上实现 self.detailLabelText.text 时,我在尝试加载表时收到一条错误消息。下面是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];

    }

    switch (pickerSelection)
    {
        case 0:
            cell.textLabel.text = [visitList1 objectAtIndex:indexPath.row];
           cell.detailTextLabel.text = [windows1 objectAtIndex:indexPath.row];

            break;
        case 1:
            cell.textLabel.text = [visitList2 objectAtIndex:indexPath.row];
           cell.detailTextLabel.text = [windows2 objectAtIndex:indexPath.row];
            break;

    }
    return cell;
}

我收到错误消息:

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“ * -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]”

该表使用主 textLabel 正确加载,之后我可以加载一个字符串,cell.detailTextLabel =因此我知道我的设置是正确的。我也可以 NSLog 我试图加载的两个数组,只是不在 detailLabelText 中。

我知道错误消息必须与数组的预期长度有关,我只是不知道是什么或在哪里。对不起,一个基本的问题。我是菜鸟。

更新:这是我的 numberOfRowsInSelection 方法:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    switch (pickerSelection)
    {
        case 0:
            return [visitList1 count];
            return [windows1 count];            
            break;
        case 1:
            return [visitList2 count];
            return [windows2 count];
            break;


    }

    return 10;
}

每个数组都有超过 3 个项目。我扔了最后一个“return 10”;在那里以防万一 switch 语句不能正常工作,但这似乎并不重要。

更新 2:当我用静态字符串替换我的 windows1 数组时,它们会填充在表格的详细信息区域中。太好了(尽管我现在的原始窗口数组似乎有某种问题)。

4

1 回答 1

0

数组windows1windows2数组中只有 2 个项目,并且您的表数据源告诉它有 3 行要显示。正如凯文所问的那样,使用-tableView:numberOfRowsInSection:您实施的方法编辑您的问题,因为几乎可以肯定问题就在那里。

于 2013-09-05T20:41:59.370 回答