給UITableView的cell添加長(zhǎng)按手勢(shì),可以知道cell的indexPath

用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彈框
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容