自定义AccessoriesView图像和annexButtonTappedForRowWithIndexPath [英] Custom accessoryView image and accessoryButtonTappedForRowWithIndexPath

查看:37
本文介绍了自定义AccessoriesView图像和annexButtonTappedForRowWithIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 accessoryButtonTappedForRowWithIndexPath 函数使用自定义图像.据我了解,要执行此操作,您必须使用 UIButton ,只需在 UITableViewCellAccessoryDe​​tailDisclosureButton 上添加 UIImageView 即可.

I am trying to get a custom image working with the accessoryButtonTappedForRowWithIndexPath function. From what I understand in order to do this, you have to use a UIButton, simply adding a UIImageView on the UITableViewCellAccessoryDetailDisclosureButton will not work.

我的问题是如何从我的 UITableViewCell 子类中获取触摸手势到父 UITableView 函数中.

My problem is how to get the touch gesture from my UITableViewCell subclass into the parent UITableView function.

这是我的 UITableViewCell 子类中的代码:

Here is the code in my UITableViewCell subclass:

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 的子类中)

The calling function (also inside the subclass of the UITableViewCell)

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

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

It crashes at the final line of the above function with this:

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewWrapperView delegate]: unrecognized selector sent to instance 0x16f48160'

推荐答案

是的,它应该崩溃,因为不应从自定义单元类调用tableview的委托,最好使用自定义委托.我正在发布与您的情况类似的示例代码,当点击按钮时,它调用的是自定义委托方法,您可以根据自己的意愿进行操作

Yes it should crash, because delegate of tableview should not be called from custom cell class, better you can use custom delegate. I am posting a sample code, similar to your case, when button tapped it call's the custom delegate method from that u can do whatever you want


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


 }


希望这会有所帮助

这篇关于自定义AccessoriesView图像和annexButtonTappedForRowWithIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆