我正在尝试使用accessoryButtonTappedForRowWithIndexPath
函数获取自定义图像。根据我的理解,为了做到这一点,您必须使用UIButton
,只需在UIImageView
上添加UITableViewCellAccessoryDetailDisclosureButton
即可。
我的问题是如何将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'
答案 0 :(得分: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");
}
希望这有帮助
答案 1 :(得分: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
}
答案 2 :(得分: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];
}