1

我正在尝试让自定义图像与该accessoryButtonTappedForRowWithIndexPath功能一起使用。据我了解,为了做到这一点,你必须使用 a UIButton,简单地添加 aUIImageViewUITableViewCellAccessoryDetailDisclosureButton行不通的。

我的问题是如何将触摸手势从我的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”

4

3 回答 3

3

是的,它应该崩溃,因为不应从自定义单元类调用 tableview 的委托,最好您可以使用自定义委托。我发布了一个示例代码,类似于你的情况,当点击按钮时,它调用自定义委托方法,你可以做任何你想做的事情


   //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");


 }


希望这会有所帮助

于 2013-08-26T09:22:38.000 回答
0

一个更好的怎么样?

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
}
于 2014-05-15T14:20:36.960 回答
0

我在 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];
}
于 2015-07-27T12:43:17.210 回答