今天做了个项目,里面使用到相同布局的多个TableView,然后就想,自定义UITableViewCell,再加到各个UITableView上。在cell上加了几个按钮,按钮的实现方法要写在UITableView所在的控制器里,这里需要使用协议,将按钮要实现的方法写到协议里,控制器遵守协议,实现按钮的方法。但是总是报错,研究了好长时间,才发现问题所在。原因是:
[self.wish addTarget:self action:@selector(buttonClickedByTag:) forControlEvents:UIControlEventTouchUpInside];
Target对象不应该是self , 而应该是self.delegate. 让遵守协议的控制器来执行这个方法。具体代码如下;
在ShoppingCell.h头文件中
#import <UIKit/UIKit.h>
#import "ShoppingCellDelegate.h"
@class HomeViewController;
@interface ShoppingCell : UITableViewCell
{
UIButton * _love;
UIButton * _wish;
UIButton * _critical;
id<ShoppingCellDelegate> _delegate;
}
@property(retain,nonatomic) UIButton * love;
@property(retain,nonatomic) UIButton * wish;
@property(retain,nonatomic) UIButton * critical;
@property(assign,nonatomic) id<ShoppingCellDelegate> delegate;
-(void)creatView;
@end
在ShoppingCell.m的实现文件中
#import "ShoppingCell.h"
#import "HomeViewController.h"
@implementation ShoppingCell
@synthesize love=_love,wish=_wish,critical=_critical;
@synthesize delegate=_delegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self creatView];
}
return self;
}
-(void)creatView
{
//添加喜欢按钮
self.love=[UIButton buttonWithType:UIButtonTypeCustom];
//目前先屏蔽 后来会用
// //love.tag=tag;//从服务器传回喜欢的tag值
// if (love.tag==100) {
// [love setBackgroundImage:[UIImage imageNamed:@"屏幕快照 2013-06-26 上午10.52.16.png"] forState:UIControlStateNormal];
// }
// else if(love.tag==99)
// {
// [love setBackgroundImage:[UIImage imageNamed:@"c75c10385343fbf29df3a033b17eca8064388fc6.jpg"] forState:UIControlStateNormal];
// }
self.love.frame=CGRectMake(5, 5, 30, 30);
self.love.tag=100;
[self.love setBackgroundImage:[UIImage imageNamed:@"屏幕快照 2013-06-26 上午10.52.16.png"] forState:UIControlStateNormal];
[self.love addTarget:self.delegate action:@selector(buttonClickedByTag:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.love];
//添加心愿单按钮
self.wish=[UIButton buttonWithType:UIButtonTypeCustom];
[self.wish setBackgroundImage:[UIImage imageNamed:@"心愿单.png"] forState:UIControlStateNormal];
self.wish.frame=CGRectMake(100, 0, 40, 38);
self.wish.tag=101;
[self.wish addTarget:self.delegate action:@selector(buttonClickedByTag:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.wish];
//添加评论按钮
self.critical=[UIButton buttonWithType:UIButtonTypeCustom];
[self.critical setBackgroundImage:[UIImage imageNamed:@"评论.png"] forState:UIControlStateNormal];
self.critical.frame=CGRectMake(270, 0, 40, 40);
self.critical.tag=102;
[self.critical addTarget:self.delegate action:@selector(buttonClickedByTag:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.critical];
}
自定义的协议
#import <Foundation/Foundation.h>
@protocol ShoppingCellDelegate <NSObject>
@required
-(void)buttonClickedByTag:(id)sender ;
@end
在控制器中使用的方法,首先引入协议头文件,遵守协议后,在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中加入以下代码:
ShoppingCell * cell=[tableView dequeueReusableCellWithIdentifier:cell1];
if (cell==nil) {
cell=[[[ShoppingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell1] autorelease];
cell.delegate=self;
//选中cell时的颜色
cell.selectionStyle=UITableViewCellSelectionStyleGray;
return cell;