UITableView 长按cell 操作 置顶

lvmw7954 7年前
   <p>做实验就简单的在tableViewController中写了下,实际上如果很多cell都需要长按操作的话,应该将这些功能封装到baseCell中,将需要添加的操作(例如数据处理)以block的方式传递,回头写好了再补发吧~</p>    <h3><strong>1. 简单的实现类似微信消息置顶的功能:</strong></h3>    <pre>  <code class="language-objectivec">#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</code></pre>    <h3><strong>2. tableView的dataSource方法</strong></h3>    <pre>  <code class="language-objectivec">// 返回多少行  - (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;  }</code></pre>    <h3><strong>3. 长按调用方法</strong></h3>    <pre>  <code class="language-objectivec">- (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];         }  }</code></pre>    <h3><strong>4. 弹出框点击调用方法</strong></h3>    <pre>  <code class="language-objectivec">- (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];  }</code></pre>    <h3><strong>5. 保存模型数组方法</strong></h3>    <pre>  <code class="language-objectivec">- (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];  }</code></pre>    <h3><strong>6. 懒加载</strong></h3>    <pre>  <code class="language-objectivec">- (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;  }</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/6b319db3b3dd</p>    <p> </p>