界面用TableView实现,其中有两个定制的cell,cell里放置了几个SDWebImageView并添加点击手势来捕获事件。开始写的时候,只是在定制Cell类里定义了界面,而后在引用的Controller里添加手势。发现代码重复了,就换用Delegate实现,整洁多了。
界面用一个tableview实现,上面的图片是tableview的HeadView。Cell里的图片能够点击,push到下个界面,点击事件绑定在Cell的类里。在引用cell的类里给cell设置tag,区分点击事件的来源。
.h文件segmentfault
@class StragetyMainPageCell; @protocol MainPageCellDelegate <NSObject> - (void)ImageTap1:(StragetyMainPageCell *)cell; - (void)ImageTap2:(StragetyMainPageCell *)cell; - (void)ImageTap3:(StragetyMainPageCell *)cell; - (void)ImageTap4:(StragetyMainPageCell *)cell; - (void)ImageTap5:(StragetyMainPageCell *)cell; - (void)moreButtonClick:(StragetyMainPageCell *)cell; @end @interface StragetyMainPageCell : UITableViewCell @property (assign, nonatomic) id<MainPageCellDelegate> delegate; @end
.m文件:
给其中一个图像添加点击事件:atom
UITapGestureRecognizer *imageTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageTap1)]; [self.image1 addGestureRecognizer:imageTap1];
具体的点击事件:spa
- (void)ImageTap1 { if (_delegate && [_delegate respondsToSelector:@selector(ImageTap1:)]) { [_delegate ImageTap1:self]; } }
就能够触发在引用类里的代理方法了。一开始不明白为何代理方法的第一个参数要是定义代理的类,这边能够经过这个cell类的tag,来区分不一样点击事件的来源。