替UITableViewCell增加按钮及处理按钮事件的方法
为UITableViewCell增加按钮及处理按钮事件的方法
根据按钮的位置获得行数:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { UIImage *image = [UIImage imageNamed:@"test.png"]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); button.frame = frame; [button addTarget:self action:@selector(accessoryViewButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [button setBackgroundImage:image forState:UIControlStateNormal]; cell.accessoryView = button; }
根据按钮的位置获得行数:
- (void)accessoryViewButtonClicked:(id)sender { CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint]; //... }