【问题标题】:Getting error, when trying to get indexPath of UITableViewCell尝试获取 UITableViewCell 的 indexPath 时出现错误
【发布时间】:2015-03-17 11:05:55
【问题描述】:

我在 UITableViewCell 中添加了一个 UIButton,在该按钮的操作中,我添加了以下代码来获取单元格的 indexPath。但它一直返回错误 - 'NSInvalidArgumentException',原因:'-[UITableViewCell indexPathForCell:]: unrecognized selector sent to instance 0x7cea3fc0'

谁能告诉我该怎么做?

- (IBAction)navigateMap:(id)sender {
    __strong UIButton *button=(UIButton *) sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    //rest of the logic
}

完整代码

- (IBAction)navigateMap:(id)sender {
__strong UIButton *button=(UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
CombinedStations *inButton=[[CombinedStations alloc] init];
inButton=[_finalStationList objectAtIndex:indexPath.section];
_universalRegion->center.latitude=inButton.latitude;
_universalRegion->center.longitude=inButton.longitude;
NSString *launchUrl=@"";
launchUrl= [launchUrl stringByAppendingString:@"http://maps.google.com/maps?daddr="];
NSString *tmpLat = [[NSString alloc] initWithFormat:@"%f", _universalRegion->center.latitude];
NSString *tmpLong = [[NSString alloc] initWithFormat:@"%f", _universalRegion->center.longitude];
NSString *llat=[tmpLat stringByAppendingString:[@"," stringByAppendingString:tmpLong]];
launchUrl=[@"http://maps.google.com/maps?daddr=" stringByAppendingString:llat];
launchUrl=[launchUrl stringByAppendingString:[@"&saddr=" stringByAppendingString:@"Current Location"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[launchUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

【问题讨论】:

  • 也许,只是也许,UITableViewCell 没有实现该方法。也许你应该使用指向 UITableView 的指针。
  • 提示:navigateMap: 在另一个班级。该类可以存储指针。

标签: ios objective-c xcode uitableview


【解决方案1】:

试试这个,这将是获取 table 的 indexpath 的非常简单的方法。

- (IBAction)navigateMap:(id)sender {

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableViewName];
    NSIndexPath *indexPath = [tableViewName indexPathForRowAtPoint:buttonPosition];
}

【讨论】:

  • 你放NSLog检查了吗?并且发件人不是空的吗?
  • 应用程序在NSIndexPath *indexPath = [tableViewName indexPathForRowAtPoint:buttonPosition]; } 之后立即崩溃,不,它不是空的..table 视图和单元格不是“nil”
  • 你的表名是什么?
  • tableView 是操作中使用的名称..您想要整个代码吗?
  • 好吧..我会先简单地告诉上下文。我放了一个按钮来启动“在地图中导航”页面。单元格为“火车站”文件加载一个对象。当我点击按钮时,它包含我想要访问的纬度和经度
【解决方案2】:

button 的超级视图是单元格的contentView。尝试获取按钮的超级视图的超级视图:

UITableViewCell *cell = button.superview.superview;

恕我直言,这是一个非常糟糕的设计。

【讨论】:

    【解决方案3】:

    尝试从tableView中获取indexpath。只需为该单元格创建一个委托,并在点击按钮时触发它。

    符合tableView所有者内的委托。每当您捕获回调时,都会向 tableview 询问来自委托的传入单元格的 indexPath。

    - (void)cell:(SampleCell *)cell actionButtonTapped:(UIButton *)button
    {
        self.currentIndexPath = [self.tableView indexPathForCell:cell];
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 2021-10-01
      • 1970-01-01
      • 2022-06-23
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      相关资源
      最近更新 更多