用XIB自定义的Cell(MessageCell) 里面带有长按手势
MessageCell.h
文件
#import <UIKit/UIKit.h>
@class EachMessageModel;
@interface MessageCell : UITableViewCell
@property (nonatomic, strong) EachMessageModel *messageModel;
/**
添加长按手势
@param target 手势作用者
@param action 响应方法
*/
- (void)addLongGes:(id)target action:(SEL)action;
@end
MessageCell.m
文件
#import "MessageCell.h"
#import "MessageModel.h"
@interface MessageCell()
/**消息背景图*/
@property (weak, nonatomic) IBOutlet UIImageView *messageBGView;
/**消息标题*/
@property (weak, nonatomic) IBOutlet UILabel *messageTitleL;
/**消息简介*/
@property (weak, nonatomic) IBOutlet UILabel *messageSubL;
/**消息日期*/
@property (weak, nonatomic) IBOutlet UILabel *messageDateL;
/**已读 未读*/
@property (weak, nonatomic) IBOutlet UILabel *isRead;
/**小红点*/
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation MessageCell
- (void)awakeFromNib {
[super awakeFromNib];
self.contentView.backgroundColor = [UIColor colorFromHexRGB:@"f2f2f2"];
self.redView.layer.cornerRadius = 3.5;
self.redView.layer.masksToBounds = true;
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.messageBGView.image = [UIImage imageWithOriginalName:@"messageBG"].resizbleImage;
}
- (void)setMessageModel:(EachMessageModel *)messageModel{
_messageModel = messageModel;
self.messageTitleL.text = messageModel.title;
self.messageDateL.text = [commonTools trameformDateStr:messageModel.createDate];
self.messageSubL.text = messageModel.intro;
//是否已读 0.未读 1.已读
if (messageModel.isRead.integerValue == 0) {
self.redView.hidden = false;
self.isRead.text = @"未读";
}else{
self.redView.hidden = true;
self.isRead.text = @"已读";
}
[self.contentView layoutIfNeeded];
messageModel.rowHeight = self.isRead.mj_y + self.isRead.mj_h + 19;
}
// 长按手势
- (void)addLongGes:(id)target action:(SEL)action{
UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc]initWithTarget:target action:action];
//设定最小的长按时间 按不够这个时间不响应手势
longGes.minimumPressDuration = 1;
[self.contentView addGestureRecognizer:longGes];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
在cellForRowAtIndexPath方法里面添加长按手势作用者和响应方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EachMessageModel *eachModel = self.dataArr[indexPath.row];
MessageCell *messageCell = [tableView dequeueReusableCellWithIdentifier:messageCellID forIndexPath:indexPath];
[messageCell addLongGes:self action:@selector(longGes:)]; // 在这里添加手势的作用者 和 响应方法
messageCell.messageModel = eachModel;
return messageCell;
}
#pragma mark - 长按删除事件
- (void)longGes:(UILongPressGestureRecognizer *)longGes{
if (longGes.state == UIGestureRecognizerStateBegan) {//手势开始
CGPoint point = [longGes locationInView:self.messageTableView];
NSIndexPath *index = [self.messageTableView indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按
self.selectMessage = index.row;
}
if (longGes.state == UIGestureRecognizerStateEnded){//手势结束
self.alertView.hidden = false; // 删除Alert弹框
}
}