在我的iOS5 UITableView中,dequeueReusableCellWithIdentifier出现错误

49

我在iOS 5上遇到了这个错误:

-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0xa217200

但是,在iOS 6上没有任何错误。我该如何解决这个问题?以下是我的代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; /// SIGABRT error

    if (!cell)
    {
        cell = [[UITableViewCell alloc]
        initWithStyle: UITableViewCellStyleSubtitle
        reuseIdentifier: CellIdentifier];
    }

    return cell;
}

请将您的tableView代理方法粘贴在这里。 - Aniruddh
提供足够的代码来得到答案。 - Nayan
2个回答

128

编辑: 这个方法是在iOS6+ SDK中新添加的。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

但在iOS 5中,为了创建UITableViewCell实例,我们通常使用以下方法:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

iOS 5中不需要使用iOS 6中使用的额外参数(forIndexPath:),因此请更改您的方法。这样将可以正常工作。


34
感谢 Apple 制作了 UITableViewController 的默认模板代码并使用了仅适用于 iOS6 的方法,却没有告诉我们! - Ben Clayton

7

以下是你遇到错误的原因。根据iOS 6.0文档集,UITableView类参考说明dequeueReusableCellWithIdentifier:可用于iOS 2.0及更高版本,dequeueReusableCellWithIdentifier:forIndexPath:可用于iOS 6.0及更高版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接