UITableView 長按cell 操作 置頂

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

  • (BOOL)canBecomeFirstResponder{
    return YES;
    }

1. 簡單的實現類似微信消息置頂的功能:

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

2. tableView的dataSource方法

// 返回多少行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// 判斷置頂模型數組是或否有值決定Section數
    if (self.topModelArr.count > 0) {
        return 2;
    } else {
        return 1;
    }
}
// 返回每一組有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 判斷是否有置頂數組
    if (self.topModelArr.count > 0) {
// 有則section=0是返回置頂數
        if (section == 0) {
            return self.topModelArr.count;
        }
    }
// 沒有直接返回非置頂數,或者section=1時返回非置頂數
    return self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.創建自定義cell
    baseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseCell"];
    if (self.topModelArr.count == 0) {
        // 2.設置數據
        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. 長按調用方法

- (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做為第一響應(cell默認是無法成為responder,需要重寫canBecomeFirstResponder方法)
        [cell becomeFirstResponder];
        // 創建提示框
        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. 彈出框點擊調用方法

- (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. 保存模型數組方法

- (void)saveModelArr {
    // 拼接歸檔文件路徑(自己謝了一個文檔拼接的NSString分類,大家可以自己拼一下)
  NSString *filePath = [KFilePath appendDocumentPath];
    // 歸檔"歸檔數組時它會把數組中的每一個對象拿一個一個的去歸"
    [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];
        // 先解檔取數據,可能取不到數據
       self.modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        
        // 如果還為空就手動實例化數據
        if (_modelArr == nil) {
            _modelArr = [NSMutableArray array];
        }
    }
    return _modelArr;
}

- (NSMutableArray *)topModelArr{
    if (_topModelArr == nil) {
        NSString *filePath = [KTopFilePath appendDocumentPath];
        // 先解檔取數據,可能取不到數據
        self.topModelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        // 如果還為空就手動實例化數據
        if (_topModelArr == nil) {
            _topModelArr = [NSMutableArray array];
        }
    }
    return _topModelArr;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容