【发布时间】: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