UITableView 長按cell 操作 置頂

做實(shí)驗(yàn)就簡單的在tableViewController中寫了下,實(shí)際上如果很多cell都需要長按操作的話,應(yīng)該將這些功能封裝到baseCell中,將需要添加的操作(例如數(shù)據(jù)處理)以block的方式傳遞,回頭寫好了再補(bǔ)發(fā)吧~
cell的.m文件必須則實(shí)現(xiàn)下面這個(gè)方法

  • (BOOL)canBecomeFirstResponder{
    return YES;
    }

1. 簡單的實(shí)現(xiàn)類似微信消息置頂?shù)墓δ?

#define KFilePath @"model"
#define KTopFilePath @"topModel"
@interface viewController ()
// 未置頂cell模型數(shù)組
@property (nonatomic, strong) NSMutableArray *modelArr;
// 置頂數(shù)據(jù)
@property (nonatomic, strong) NSMutableArray *topModelArr;
// 被選中cell的IndexPath;
@property (nonatomic, strong) NSIndexPath *selectIndexPath;
@end

2. tableView的dataSource方法

// 返回多少行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// 判斷置頂模型數(shù)組是或否有值決定Section數(shù)
    if (self.topModelArr.count > 0) {
        return 2;
    } else {
        return 1;
    }
}
// 返回每一組有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 判斷是否有置頂數(shù)組
    if (self.topModelArr.count > 0) {
// 有則section=0是返回置頂數(shù)
        if (section == 0) {
            return self.topModelArr.count;
        }
    }
// 沒有直接返回非置頂數(shù),或者section=1時(shí)返回非置頂數(shù)
    return self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.創(chuàng)建自定義cell
    baseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseCell"];
    if (self.topModelArr.count == 0) {
        // 2.設(shè)置數(shù)據(jù)
        cell.contact = self.modelArr[indexPath.row];
        // 3.返回
    } else {
        if (indexPath.section == 0) {
        // 也可在這里自己注冊兩種不同樣式的cell
            cell.model = self.topModelArr[indexPath.row];
        } else {
            cell.model = self.modelArr[indexPath.row];
        }
    }
// 4 添加長按手勢操作
    UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPress:)];
    [cell addGestureRecognizer:longPressGesture];
    return cell;
}

3. 長按調(diào)用方法

- (void)cellLongPress:(UIGestureRecognizer *)recognizer{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        CGPoint location = [recognizer locationInView:self.tableView];
        self.selectIndexPath = [self.tableView indexPathForRowAtPoint:location];
        HMContactCell *cell = (HMContactCell *)recognizer.view;
        //這里把cell做為第一響應(yīng)(cell默認(rèn)是無法成為responder,需要重寫canBecomeFirstResponder方法)
        [cell becomeFirstResponder];
        // 創(chuàng)建提示框
        UIMenuItem *itCopy = [[UIMenuItem alloc] initWithTitle:@"置頂" action:@selector(handleCopyCell:)];
        UIMenuItem *itDelete = [[UIMenuItem alloc] initWithTitle:@"移除置頂" action:@selector(handleDeleteCell:)];
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setMenuItems:[NSArray arrayWithObjects:itCopy, itDelete,  nil]];
        [menu setTargetRect:cell.frame inView:self.tableView];
        [menu setMenuVisible:YES animated:YES];   
    }
}

4. 彈出框點(diǎn)擊調(diào)用方法

- (void)handleCopyCell:(id)sender{//置頂cell
    
    [self.topContacts addObject:self.contacts[self.selectIndexPath.row]];//
    [self saveTopModelArr];
    [self.tableView reloadData];
}
- (void)handleDeleteCell:(id)sender{//移除置頂cell
    
    [self.topContacts removeObjectAtIndex:self.selectIndexPath.row];
    [self savetopModelArr];
    [self.tableView reloadData];
}

5. 保存模型數(shù)組方法

- (void)saveModelArr {
    // 拼接歸檔文件路徑(自己謝了一個(gè)文檔拼接的NSString分類,大家可以自己拼一下)
  NSString *filePath = [KFilePath appendDocumentPath];
    // 歸檔"歸檔數(shù)組時(shí)它會(huì)把數(shù)組中的每一個(gè)對象拿一個(gè)一個(gè)的去歸"
    [NSKeyedArchiver archiveRootObject:self.contacts toFile:filePath];
}
- (void)saveTopModelArr {
    // 拼接歸檔文件路徑
    NSString *filePath = [KTopFilePath appendDocumentPath];
    [NSKeyedArchiver archiveRootObject:self.topContacts toFile:filePath];
}

6. 懶加載

- (NSMutableArray *)modelArr {
    if (_modelArr == nil) {
        NSString *filePath = [KFilePath appendDocumentPath];
        // 先解檔取數(shù)據(jù),可能取不到數(shù)據(jù)
       self.modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        
        // 如果還為空就手動(dòng)實(shí)例化數(shù)據(jù)
        if (_modelArr == nil) {
            _modelArr = [NSMutableArray array];
        }
    }
    return _modelArr;
}

- (NSMutableArray *)topModelArr{
    if (_topModelArr == nil) {
        NSString *filePath = [KTopFilePath appendDocumentPath];
        // 先解檔取數(shù)據(jù),可能取不到數(shù)據(jù)
        self.topModelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        // 如果還為空就手動(dòng)實(shí)例化數(shù)據(jù)
        if (_topModelArr == nil) {
            _topModelArr = [NSMutableArray array];
        }
    }
    return _topModelArr;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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