默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。
- UIButton *button;
- if(isEditableOrNot) {
- UIImage *p_w_picpath = [UIImage p_w_picpathNamed:@"delete.png"];
- button = [UIButton buttonWithType:UIButtonTypeCustom];
- CGRect frame = CGRectMake(0.0,0.0,p_w_picpath.size.width,p_w_picpath.size.height);
- button.frame = frame;
- [button setBackgroundImage:p_w_picpath forState:UIControlStateNormal];
- button.backgroundColor = [UIColor clearColor];
- cell.accessoryView = button;
- }else{
- button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.backgroundColor = [UIColor clearColor];
- cell.accessoryView = button;
- }
[button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后, 虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参 数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已 经无法做到了。
- // 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- - (void)btnClicked:(id)sender event:(id)event
- {
- NSSet *touches = [event allTouches];
- UITouch *touch = [touches anyObject];
- CGPoint currentTouchPosition = [touch locationInView:self.tableView];
- NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
- if(indexPath != nil)
- {
- [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
- }
- }
- - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
- {
- int *idx = indexPath.row;
- //这里加入自己的逻辑
- }