Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate

后端 未结 10 2036
独厮守ぢ
独厮守ぢ 2020-11-27 09:35

I\'m using a custom drawn UITableViewCell, including the same for the cell\'s accessoryView. My setup for the accessoryView happens by the way of something like

10条回答
  •  离开以前
    2020-11-27 09:58

    My approach is to create a UITableViewCell subclass and encapsulate the logic that will call the usual UITableViewDelegate's method within it.

    // CustomTableViewCell.h
    @interface CustomTableViewCell : UITableViewCell
    
    - (id)initForIdentifier:(NSString *)reuseIdentifier;
    
    @end
    
    // CustomTableViewCell.m
    @implementation CustomTableViewCell
    
    - (id)initForIdentifier:(NSString *)reuseIdentifier;
    {
        // the subclass specifies style itself
        self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        if (self) {
            // get the button elsewhere
            UIButton *accBtn = [ViewFactory createTableViewCellDisclosureButton];
            [accBtn addTarget: self
                       action: @selector(accessoryButtonTapped:withEvent:)
             forControlEvents: UIControlEventTouchUpInside];
            self.accessoryView = accBtn;
        }
        return self;
    }
    
    #pragma mark - private
    
    - (void)accessoryButtonTapped:(UIControl *)button withEvent:(UIEvent *)event
    {
        UITableViewCell *cell = (UITableViewCell*)button.superview;
        UITableView *tableView = (UITableView*)cell.superview;
        NSIndexPath *indexPath = [tableView indexPathForCell:cell];
        [tableView.delegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
    }
    
    @end
    

提交回复
热议问题