用XIB自定義的Cell(MessageCell) 里面帶有長(zhǎng)按手勢(shì)
MessageCell.h
文件
#import <UIKit/UIKit.h>
@class EachMessageModel;
@interface MessageCell : UITableViewCell
@property (nonatomic, strong) EachMessageModel *messageModel;
/**
添加長(zhǎng)按手勢(shì)
@param target 手勢(shì)作用者
@param action 響應(yīng)方法
*/
- (void)addLongGes:(id)target action:(SEL)action;
@end
MessageCell.m
文件
#import "MessageCell.h"
#import "MessageModel.h"
@interface MessageCell()
/**消息背景圖*/
@property (weak, nonatomic) IBOutlet UIImageView *messageBGView;
/**消息標(biāo)題*/
@property (weak, nonatomic) IBOutlet UILabel *messageTitleL;
/**消息簡(jiǎn)介*/
@property (weak, nonatomic) IBOutlet UILabel *messageSubL;
/**消息日期*/
@property (weak, nonatomic) IBOutlet UILabel *messageDateL;
/**已讀 未讀*/
@property (weak, nonatomic) IBOutlet UILabel *isRead;
/**小紅點(diǎn)*/
@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;
}
// 長(zhǎng)按手勢(shì)
- (void)addLongGes:(id)target action:(SEL)action{
UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc]initWithTarget:target action:action];
//設(shè)定最小的長(zhǎng)按時(shí)間 按不夠這個(gè)時(shí)間不響應(yīng)手勢(shì)
longGes.minimumPressDuration = 1;
[self.contentView addGestureRecognizer:longGes];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
在cellForRowAtIndexPath方法里面添加長(zhǎng)按手勢(shì)作用者和響應(yīng)方法
- (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:)]; // 在這里添加手勢(shì)的作用者 和 響應(yīng)方法
messageCell.messageModel = eachModel;
return messageCell;
}
#pragma mark - 長(zhǎng)按刪除事件
- (void)longGes:(UILongPressGestureRecognizer *)longGes{
if (longGes.state == UIGestureRecognizerStateBegan) {//手勢(shì)開(kāi)始
CGPoint point = [longGes locationInView:self.messageTableView];
NSIndexPath *index = [self.messageTableView indexPathForRowAtPoint:point]; // 可以獲取我們?cè)谀膫€(gè)cell上長(zhǎng)按
self.selectMessage = index.row;
}
if (longGes.state == UIGestureRecognizerStateEnded){//手勢(shì)結(jié)束
self.alertView.hidden = false; // 刪除Alert彈框
}
}