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

后端 未结 10 2001
独厮守ぢ
独厮守ぢ 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:53

    When the button is tapped, you could have it call the following method inside a UITableViewCell subclass

     -(void)buttonTapped{
         // perform an UI updates for cell
    
         // grab the table view and notify it using the delegate
         UITableView *tableView = (UITableView *)self.superview;
         [tableView.delegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:[tableView indexPathForCell:self]];
    
     }
    
    0 讨论(0)
  • 2020-11-27 09:56

    I found this website to be very helpful: custom accessory view for your uitableview in iphone

    In short, use this in cellForRowAtIndexPath::

    UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
    
    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 addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
    

    then, implement this method:

    - (void)checkButtonTapped:(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];
        }
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 10:04

    As of iOS 3.2 you can avoid the buttons that others here are recommending and instead use your UIImageView with a tap gesture recognizer. Be sure to enable user interaction, which is off by default in UIImageViews.

    0 讨论(0)
提交回复
热议问题