Ios “的@界面不可见”;UITableView“;声明选择器";cellForRowAtIndexPath:“单元格路径”;

Ios “的@界面不可见”;UITableView“;声明选择器";cellForRowAtIndexPath:“单元格路径”;,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我是IOS6dev的新手。我在UITableView上遇到了问题。下面的代码是在所选行的末尾显示复选标记。但是我收到了一个错误,如“对于UITableView声明选择器cellforrowatinexpath:”,没有可见的@接口UITableView具有cellForRowAtIndexPath方法,但tableView不能 展示它。我不知道为什么。请帮忙 代码如下: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexP

我是
IOS6
dev的新手。我在
UITableView
上遇到了问题。下面的代码是在所选行的末尾显示复选标记。但是我收到了一个错误,如“对于
UITableView
声明选择器
cellforrowatinexpath:
”,没有可见的@接口
UITableView
具有
cellForRowAtIndexPath
方法,但tableView不能 展示它。我不知道为什么。请帮忙

代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];   -----error line
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
问题是“tableView”无法识别UITableView下的所有方法。它知道一些,如“numberOfRowsInSection”。我不明白为什么。

TableView本身没有实现此选择器。 此方法的完整选择器为

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
并且来自代理的协议。您的代理(例如viewController)必须实现此方法。不建议(也不容易)从表中获取单元格对象

相反,请更改基础数据并使用

[tableView reloadData];

您的问题不在包含的代码示例中。你的问题在别处。我们无法根据这一个片段诊断问题。您必须与我们共享一个更完整的代码示例


与您的问题无关,您的
didSelectRowAtIndexPath
中存在一个微妙的问题。您不应该只在此处更新
cellAccessoryType
。您确实应该更新支持UI的模型。如果表中的行数超过任何给定时刻的可见行数,则这一点至关重要

为了说明这个想法,让我们假设您的模型是一个具有两个属性的对象数组,即单元格的
标题
,以及单元格是否被选中

因此,您的
cellforrowatinexpath
可能看起来像:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    RowData *rowObject = self.objects[indexPath.row];
    cell.textLabel.text = rowObject.title;

    if (rowObject.isSelected)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RowData *rowObject = self.objects[indexPath.row];
    rowObject.selected = !rowObject.isSelected;

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
而您的
didSelectRowAtIndexPath
可能如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    RowData *rowObject = self.objects[indexPath.row];
    cell.textLabel.text = rowObject.title;

    if (rowObject.isSelected)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RowData *rowObject = self.objects[indexPath.row];
    rowObject.selected = !rowObject.isSelected;

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
同样,编译器警告/错误无疑是源代码中的其他问题造成的,因为原始代码片段在语法上是正确的。我只是想纠正您的
didSelectRowAtIndexPath
中的另一个缺陷。在MVC编程中,您确实希望确保您正在更新模型(然后更新视图),而不仅仅是更新视图


但是,要明确的是,如果您不纠正导致当前编译器警告/错误的错误,那么不管您在
didSelectRowAtIndexPath
中输入了什么,您可能只会收到另一个警告。您必须确定编译器为什么对当前代码犹豫不决。

indexPath.row而不是错误行中的indexPath。.您是如何创建
UITableView
的实例的?@lakesh这是一个错误。这是,但那是离题的,所以我忽略了它:p你发布的代码没有问题。这是你的代码的真实复制和粘贴吗?通常这种错误是因为拼写问题。这里说它应该+1,因为这解决了一个微妙的问题,即您肯定应该更改基础模型,
tableview:cellforrowatinexpath:
应该根据该模型中的数据设置复选标记。非常重要。但是,-1因为我不同意重新加载整个表。糟糕的用户体验。最多,
重新加载rowsatindexpaths
。然而,关键问题是OP的代码很好。他眼前的问题不是这种方法。它在别处休息。谢谢你们。通过将xcode从4.6.2更新到4.6.3,问题得以解决。这很奇怪,我不知道为什么它会起作用。真奇怪。