我正在尝试按照Apple的示例使用自定义披露按钮。我正在做以下
- (void) customButtonTapped:(UIButton *)sender event: (id) event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
NSLog(@"index path %@", indexPath);
if (indexPath != nil)
{
}
}
问题是索引路径总是为零。每一次都没有失败。还有其他人遇到过这个吗?
修改 这就是我添加按钮的方式
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
UIImage *image = [UIImage imageNamed:@"Gear"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(customButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
答案 0 :(得分:3)
如果indexPathForRowAtPoint:
为nil
,则self.tableView
的来电可以返回nil
。
确保self.tableView
已设置(例如,如果它是IBOutlet,请确保它已连接到xib中的表视图)。