iOS - TableView中的騷操作

目錄:
1.長按cell響應(yīng)事件
2.刪除某一個(gè)cell(UITableview 長按刪除)
3.點(diǎn)擊縮放cell(伸縮)(放大和縮?。?/p>

1.長按刪除

1.重寫cell類型繼承于UITableviewcell

#import <UIKit/UIKit.h>

//協(xié)議(需要上層視圖控制器操作的時(shí)候可以使用,以下是以刪除cell為事例)
@protocol NewTableViewCellDelegate<NSObject>

-(void)deleteCell;

@end

@interface NewTableViewCell : UITableViewCell//消息界面顯示的cell

//@property (strong, nonatomic) UIImageView *headImageView;

@property id<NewTableViewCellDelegate> delegate;

@end
#import "NewTableViewCell.h"

@implementation NewTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

//讓cell能成為第一響應(yīng)者來響應(yīng)事件
- (BOOL)canBecomeFirstResponder {
    return YES;
}

//為cell增加菜單欄選擇,以下包括自定義菜單欄
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
//這些是UIMenuItem里面自帶的操作方法(注意:需要顯示什么就return yes;不需要顯示的return no;
/*以下是列表:
//cut: // 剪切
copy: // 拷貝
select: // 選擇
selectAll: // 全選
paste: // 粘貼
delete: // 刪除
_promptForReplace: // Replace...
_transliterateChinese: // 簡<=>繁
_showTextStyleOptions: // B/U
_define: // Define
_addShortcut: // Learn...
_accessibilitySpeak: // Speak
_accessibilitySpeakLanguageSelection: // Speak...
_accessibilityPauseSpeaking: // Pause
_share: // 共享...
makeTextWritingDirectionRightToLeft: // 往右縮進(jìn)
makeTextWritingDirectionLeftToRight: // 往左縮進(jìn)
*/

//    if (action == @selector(delete:) || action == @selector(copy:) || action == @selector(paste:) || action == @selector(cut:)) {
//        return YES;
//    }
    

//如果有需要也可以在這里設(shè)置彈出來的標(biāo)題,前提是不能是UIMenuItem里面自帶的東西
//    [((UIMenuController *)sender).menuItems.firstObject setTitle:@"zhang"];
    
    if (action == @selector(myDelete:)) {
        return YES;
    }

    //除了自己需要顯示的返回yes,其他的全部返回no不顯示;
    return NO;
}

//點(diǎn)擊自定義菜單欄響應(yīng)的自定義方法
- (void)myDelete:(UIMenuController *)menu
{
    NSLog(@"myDelete");
    [self resignFirstResponder];
    [self.delegate deleteCell];
}

//點(diǎn)擊delete按鈕響應(yīng)的方法
- (void)delete:(UIMenuController *)menu
{
    NSLog(@"delete");
    [self resignFirstResponder];
    [self.delegate deleteCell];
}

2.tableview的數(shù)據(jù)源協(xié)議和代理協(xié)議方法

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 81 *ScaleWidth;
}

#pragma mark - tableview datasoure

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier1=@"cell1";
//使用重用機(jī)制的初始化
//    NewTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
//    if (cell == nil) {
//        cell = [[NewTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier1];
//    }

//不使用cell的重用機(jī)制的初始化
    NewTableViewCell *cell = [[NewTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier1];

    //如果需要用到代理的話這里的self必須要遵守剛才定義的協(xié)議(NewTableViewCellDelegate)
    cell.delegate = self;

    //添加長按的手勢(shì)
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGR:)];
    //設(shè)定最小的長按時(shí)間 按不夠這個(gè)時(shí)間不響應(yīng)手勢(shì)
    longPressGR.minimumPressDuration = 0.5;
    [cell addGestureRecognizer:longPressGR];

    return cell;
}

3.長按響應(yīng)的方法實(shí)現(xiàn)

-(void)longPressGR:(UILongPressGestureRecognizer *)lpGR{
    
    //UIMenuController選擇框設(shè)置
    //如果這里不設(shè)置那么只會(huì)顯示UIMenuItem中本來帶有的菜單項(xiàng)
    //(注:這里的self.menuVc的屬性是UIMenuController類的對(duì)象)
    UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"my刪除" action:@selector(myDelete:)];
    self.menuVc.menuItems = @[item1];
    
    //    獲取長按手勢(shì)發(fā)生的位置
    CGPoint point = [lpGR locationInView:self.tableView];

    // 這里可以獲取我們?cè)谀膫€(gè)cell上長按
    //(注:這里的self.index屬性是NSIndexPath類對(duì)象)
    self.index = [self.tableView indexPathForRowAtPoint:point]; 

    //獲取長按手勢(shì)發(fā)生在哪一個(gè)對(duì)象上,并讓他成為第一響應(yīng)者
    [lpGR.view becomeFirstResponder];

    //    UIMenuController *menuVc = [[UIMenuController alloc]init];//不能在這里初始化,不然會(huì)導(dǎo)致內(nèi)存泄漏
    
    //設(shè)置菜單欄顯示的位置
    [self.menuVc setTargetRect:lpGR.view.frame inView:lpGR.view.superview];
    
    //如果還在顯示那就不初始化,不然菜單欄會(huì)一直閃
    
    if (self.menuVc.isMenuVisible)return;
    
    //顯示菜單欄
    [self.menuVc setMenuVisible:YES animated:YES];
    
     
//手勢(shì)開始的狀態(tài)
//    if (lpGR.state == UIGestureRecognizerStateBegan) {

//    }

//手勢(shì)結(jié)束的狀態(tài)
//    if (lpGR.state == UIGestureRecognizerStateEnded){

}

4.自定義協(xié)議的代理方法的實(shí)現(xiàn)

#pragma mark - newtableViewCell delegate
//刪除cell的方法
-(void)deleteCell{
    
    //刪除對(duì)應(yīng)cell,手動(dòng)管理tableView的data數(shù)據(jù)源(即cell和section的個(gè)數(shù))先刪除數(shù)據(jù)再刪除對(duì)應(yīng)的cell,不然會(huì)導(dǎo)致崩潰
    [[NIMSDK sharedSDK].conversationManager deleteRecentSession:self.data[self.index.row]];
    [self.allRecentSessions removeObjectAtIndex:self.index.row];
    [self.tableView deleteRowsAtIndexPaths:@[self.index] withRowAnimation:UITableViewRowAnimationRight];
    
}

2.點(diǎn)擊cell,使得cell放大和縮?。◣?dòng)畫)

#pragma mark - tableview datasoure

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.listData) {
        return self.listData.count;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier1=@"cell1";
//    dealViewTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
//    if (cell == nil) {
//        cell = [[dealViewTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier1];
//    }

//這里一定不能使用tableviewcell的重用機(jī)制
    dealViewTableViewCell *cell = [[dealViewTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier1];

//如果在點(diǎn)擊放大之后的cell需要顯示不同的內(nèi)容,為了能在cell消失在視野中的時(shí)候仍然能保持正確的狀態(tài),重新設(shè)置cell的參數(shù)。
//(通俗來說就是我發(fā)現(xiàn)在列表上啦或者下拉的時(shí)候cell消失的時(shí)候會(huì)重新調(diào)用這個(gè)方法)
//self.listData[indexPath.row].isOpen屬性是數(shù)據(jù)數(shù)組中用于記錄cell是否被打開即時(shí)候是放大的狀態(tài)

//    if (self.listData[indexPath.row].isOpen) {
//
//        cell.imageButton.hidden = NO;
//        cell.moreButton.hidden = YES;

//    }else{
//        cell.imageButton.hidden = YES;
//            cell.moreButton.hidden = NO;
//    }

    return cell;
}
#pragma mark - tableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //(注:self.selectIndexPath屬性是self的NSIndexPath類對(duì)象用于記錄是哪一個(gè)cell被點(diǎn)擊了)
    self.selectIndexPath = indexPath;
    //動(dòng)畫刷新cell
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        //點(diǎn)擊放大到的實(shí)現(xiàn)
        if (self.selectIndexPath != nil && self.selectIndexPath == indexPath) {
            NewTableViewCell *cell = (NewTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

            if (self.listData[indexPath.row].isOpen) {
                self.listData[indexPath.row].isOpen = NO;
                //cell.imageButton.hidden = YES;
                
            }else{
                self.listData[indexPath.row].isOpen = YES;
               // cell.imageButton.hidden = NO;
                //cell.moreButton.hidden = YES;
            }
            self.selectIndexPath = nil;
        }
        
        //根據(jù)不同的狀態(tài)返回cell的高度
        if (self.listData[indexPath.row].isOpen) {
            return 200;
        }else{
            return 100;
        }
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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