【问题标题】:Trying to create function to count number of checked cells and call it from another function尝试创建函数来计算检查单元格的数量并从另一个函数调用它
【发布时间】:2012-06-29 08:44:41
【问题描述】:

所以我创建了一个函数:

-(void)checkCount:(UITableView *)tableView isIndexPathChecked:(NSIndexPath *)indexPath{
NSString *name = [[NSString alloc] init];
UITableViewCell *investigatedCell = [tableView cellForRowAtIndexPath:indexPath];
if(investigatedCell.accessoryType ==UITableViewCellAccessoryCheckmark)
    name = investigatedCell.textLabel.text;
[checkedRows addObject: name];
}

每次我尝试从另一个方法中调用此方法时都会出错。什么是正确的语法?谢谢。

【问题讨论】:

  • 知道什么样的错误会很有用。还有你在哪里声明和初始化checkedRows
  • 我不断收到的一个错误是:当我尝试从另一种方法调用它时,使用了未声明的标识符“isIndexPathChecked”。而checkedRows是我在实现中合成的头文件中的一个属性
  • 你初始化checkedRows了吗?否则它只是nil。您是否尝试从下面的方法调用checkCount:isIndexPathChecked:?在这种情况下,您必须在标头或实现中预先声明它。
  • 是的,我初始化了checkedRows。并且方法在头文件中声明并在实现文件中定义。另外,如果有区别,方法 checkCount:isIndexPathChecked: 是我文件中的最后一个方法

标签: iphone objective-c function uitableview methods


【解决方案1】:

我相信您看到的错误是因为方法调用不在您的公共头文件中。尝试添加

-(void)checkCount:(UITableView *)tableView isIndexPathChecked:(NSIndexPath *)indexPath;

到您的 .h 文件。

您可能还想仔细检查您的拼写,并确保您将方法调用发送到正确的对象。我猜是自己。

UITableView *myTable = pointer_to_tableView_instance;
NSIndexPath *indexPath = pointer_to_indexPath_to_check;
[self checkCount:myTable isIndexPathChecked:indexPath];
NSLog(@"%i",checkedRows.count);

【讨论】:

  • 如果是私有方法,不应该添加到头文件中。相反,它可以作为类扩展添加到实现文件中。
  • 我的头文件中声明了该方法。我只是不确定如何在我的实现文件中调用它。当我单击一个按钮时,我希望调用该方法,因此在 pressButton 方法中我希望它调用我的自定义方法。
  • [self checkCount:myTable isIndexPathChecked:indexPath];
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2019-06-18
  • 2020-06-25
  • 1970-01-01
  • 2017-05-18
  • 2015-06-19
  • 1970-01-01
相关资源
最近更新 更多