自定义图像accessoryView的和accessoryButtonTappedForRowWit

2019-10-18 13:16发布

我试图让一个自定义图像与工作accessoryButtonTappedForRowWithIndexPath功能。 从我在为了做到这一点理解,你必须使用一个UIButton ,简单地增加一个UIImageViewUITableViewCellAccessoryDetailDisclosureButton将无法正常工作。

我的问题是如何从我获得触摸手势UITableViewCell子成父UITableView功能。

这里是我的代码UITableViewCell子类:

upVote = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *upVoteImg = [UIImage imageNamed:@"vote.png"];
upVote.frame = CGRectMake(self.frame.size.width-upVoteImg.size.width, 0, upVoteImg.size.width , upVoteImg.size.height);
[upVote setBackgroundImage:upVoteImg forState:UIControlStateNormal];
[self.contentView addSubview:upVote];
[upVote addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];

调用函数(也是子类内部UITableViewCell

- (void)checkButtonTapped:(id)sender event:(id)event
{
    UITableView *tableView = (UITableView *)self.superview;
    [tableView.delegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:[tableView indexPathForCell:self]];
}

它崩溃在这个上面的函数的最后一行:

***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“ - [UITableViewWrapperView委托]:无法识别的选择发送到实例0x16f48160”

Answer 1:

是的,它应该崩溃,因为tableview中的代表不应自定义单元格类调用,你可以更好的使用自定义的委托。 我张贴的样本代码,类似于你的情况下,当按钮拍了拍它调用的自定义的委托方法从u可以做你想做的


   //in custom calss
   #import <UIKit/UIKit.h>

        //create a delegate
     @protocol CustomDelegate<NSObject>
     -(void)whenButtonTapped:(id)sender; //your delegate method
     @end

     @interface CustomCell : UITableViewCell<CustomDelegate>
     @property(nonatomic, assign)id<CustomDelegate> delegate; //create delegate
     @end



    //.m file of custom cell
    #import "CustomCell.h"

    @implementation CustomCell 
    @synthesize delegate; //sysnthesize delegate

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
   {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
       // Initialization code
      UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //its your button
      UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectZero]; //for example i took label

     [aButton addTarget:self action:@selector(whenButtonTapped:) forControlEvents:UIControlEventTouchUpInside];//adding target for button action
     [aButton setTag:100];
     [aLabel setTag:200];

     [self addSubview:aButton];
     [self addSubview:aLabel];

     //since i am using without ARC
     [aLabel release];
      }
    return self;
   }

  - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  {
     [super setSelected:selected animated:animated];
     // Configure the view for the selected state
  }

  -(void)dealloc
   {
     delegate = nil;
     [super dealloc];
   }

   //i am setting the frames hear
   -(void)layoutSubviews
    {
       [super layoutSubviews];
       UIButton *aButton = (UIButton *)[self viewWithTag:100]; //get button
       aButton.frame = CGRectMake(200, 5, 55, 40);
       [aButton setTitle:@"tapMe" forState:UIControlStateNormal];

       UILabel *label = (UILabel *) [self viewWithTag:200]; //get label
       label.frame = CGRectMake(40, 5, 50, 35);
       label.text = @"hello";

    }

     //hear is ur button's target
    - (void)whenButtonTapped:(id)sender
  {
     //dont call UITableView delegate method in custom cell it is wrong, ur app get crashed
      //insted create custom delegate method to your controller
    [self.delegate whenButtonTapped:sender];//call the delegate method when button tapped
  }


  @end

  //.h file where u are using custom cell

   #import <UIKit/UIKit.h>
   #import "CustomCell.h"
   @interface ViewController : UIViewController<UITableViewDataSource , UITableViewDelegate ,CustomDelegate>//set confirms to delegate
   @property (retain, nonatomic) IBOutlet UITableView *aTableView;

   @end

  //.m file

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
       CustomCell *aCell = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
       if(aCell == nil)
      {
         aCell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

     }
     aCell.delegate = self;//set delegate to this class

     return aCell;
  }

  //hear is your delegate method
  //define your delegate method hear
  -(void)whenButtonTapped:(UIButton *)sender
 {

   //in this method u can do what ever the activity when button tapped
   //sender which gives u entire cell information
   UITableViewCell *cell = sender.superview;//you can get cell also :)
   NSLog(@"button tapped");


 }


希望这有助于



Answer 2:

如何更好的呢?

UITableView *tableview;

if ([self.superview respondsToSelector:@selector(indexPathForCell:)] ) {
    tableview = (UITableView *)self.superview;
} else if ([self.superview.superview respondsToSelector:@selector(indexPathForCell:)]) {
    tableview = (UITableView *)self.superview.superview;
}

if (tableview) {
    // win
}


Answer 3:

我有一个的UITableViewCell类,看起来像这样。

- (UITableView *)tableView
{
    UIView *view = self.superview;

    while (view)
    {
        if ([view isKindOfClass:[UITableView class]])
        {
            return (UITableView *)view;
        }
        else
        {
            view = view.superview;
        }
    }

    return nil;
}

- (NSIndexPath *)indexPath
{
    return [[self tableView]indexPathForCell:self];
}

然后,我只是钩我的自定义按钮,直至在我的自定义的tableview细胞的方法(其中进口我的类别)的方法,如本。

- (IBAction)customButtonPressed:(id)sender
{
    [self.tableView.delegate tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:self.indexPath];
}


文章来源: Custom accessoryView image and accessoryButtonTappedForRowWithIndexPath