1

男士们,

我需要一些帮助。这几乎就像我知道我在尝试做什么,但是在编写代码时我一直无法让它工作。场景是我有一个视图控制器,里面有一个表视图。我的表格视图有三种不同的动态原型,因为我有三种不同的单元格。现在,我要做的就是指定在哪一行中生成哪个原型单元格。我还在情节提要中为每个原​​型单元格提供了唯一标识符。

我理解事物的方式是我的 cellforRowatIndexpath 方法需要了解在该行中显示哪种单元格,然后选择标识符,出列并根据需要设置内容。

相应地,这是我正在尝试做的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   * )indexPath
{
    if(indexPath.row==0)
    {
        static NSString *CellIdentifier = @"HelloCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault      reuseIdentifier:CellIdentifier];
        }

        // configure your cell here...

        return cell;
    }
}

不幸的是,事情没有按计划进行,我的应用程序崩溃了 2013-02-07 16:47:39.859 ProjectsABC30068:c07] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSIndexPath setTableViewStyle:] : 无法识别的选择器发送到实例 0x83812e0' 错误。

由于我已正确设置数据源和委托,因此表视图没有问题。并且还为表格视图做了一个出口。

我还尝试将 cellforRowAtIndexPath 中的 if 语句更改为 if(myTable.indexPathForSelectedRow==0)。这会在所有单元格中生成动态原型,但至少应用程序不会停止。

大家觉得是什么问题?

我不知道如何正确使用索引路径,如果有人可以帮助我,我将不胜感激。

4

3 回答 3

7

With Storyboard and Dynamic prototype cells, you don't have to worry about or check if dequeueReusableCellWithIdentifier: returns a nil cell.

What you have to do:

  • For each row, determine the correct cell identifier and feed that to dequeueReusableCellWithIdentifier:
  • For each dynamic prototype cell, make sure you setup the correct identifier (select each cell on the storyboard, then in Attributes Inspector under Table View Cell section fill Identifier)

set up cell identifier

Let's have an example. Say you have three prototype cells, with identifier as 'Cell1', 'Cell2' and 'Cell3'. And assume you have three rows, each to display with one of the prototype cells.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"Cell%d", indexPath.row + 1];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    cell.textLabel.text = identifier; // just show the identifier as title
    return cell;
}
于 2013-02-08T02:58:50.370 回答
2

You said it's reporting:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSIndexPath setTableViewStyle:]: unrecognized selector sent to instance 0x83812e0

You get this when cellForRowAtIndexPath is not returning a valid UITableViewCell.

What value did you return from tableView:numberOfRowsInSection:? The reason I ask is that your cellForRowAtIndexPath will fail for any row except the first one. If your tableView:numberOfRowsInSection: indicated a value greater than 1, then your app will crash.

To confirm this, you might want to examine what indexPath is at the the start of your cellForRowAtIndexPath, by adding this line at the start of the method:

NSLog(@"%s: section = %d; row = %d", __FUNCTION__, indexPath.section, indexPath.row);

That way you can make sure what rows you're trying to generate a cell for and that you're correctly handling it.

Later you say:

I've also tried to change the if statement in cellforRowAtIndexPath to if(myTable.indexPathForSelectedRow==0). This produces the dynamic prototype in all the cells but at least the app doesn't cease.

That definitely isn't going to work and it implies a confusion of how cellForRowAtIndexPath works. It has nothing to do with currently selected cells. You respond to numberOfSectionsInTableView and tableView:numberOfRowsInSection: by telling it which cells your are able to create, and iOS will then call cellForRowAtIndexPath repeatedly for each of the cells it determines might be visible given the number of sections and number of rows.

Make sure that your numberOfSectionsInTableView and tableView:numberOfRowsInSection: methods only return values that you correctly handle in your cellForRowAtIndexPath.


For further reading, I'd suggest you go through the Table View Programming Guide for iOS or google "UITableView tutorial".

于 2013-02-08T03:01:41.803 回答
1

(作为评论留下的时间有点长,所以我把它放在这里,即使它没有直接回答问题)

异常断点:
转到断点导航器
转到断点导航器

在导航器的底部,您有“+”按钮
添加断点

然后它将添加一个断点供您自定义,只需单击完成即可。
点击完成完成

现在,当抛出异常时,您将被带到触发异常的行。最重要的是,您将能够看到那一刻的整个调用堆栈。对调试非常有用。

自 4.2 以来,Xcode 中的断点可以完成更多“时髦”的东西(不确定具体时间)

于 2013-02-08T02:46:20.347 回答