【无限互联】框架学习——MCSwipeTableViewCell
来源:互联网 发布:高压柜模型实时数据 编辑:程序博客网 时间:2024/06/03 16:17
本次学习的MCSwipeTableViewCell的效果如图:
MCSwipeTableViewCell是表视图单元格上滑动手势的运用,类似Mailbox App的。可自定义颜色和图标,默认滑动删除内容。要求iOS 5及以上版本,支持ARC。
一、MCSwipeTableViewCell的使用
1.首先将MCSwipeTableViewCell的.m和.h文件与相应的图片文件夹导入
2.创建tableView并实现其代理
3.返回单元格的方法实现
//返回单元格- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; MCSwipeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[MCSwipeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; cell.contentView.backgroundColor = [UIColor whiteColor]; } //配置单元格 [self configureCell:cell forRowAtIndexPath:indexPath]; return cell;}
4.配置单元格的方法实现
//配置单元格- (void)configureCell:(MCSwipeTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { //设置选中视图 UIView *checkView = [self viewWithImageName:@"check"]; UIColor *greenColor = [UIColor colorWithRed:85.0 / 255.0 green:213.0 / 255.0 blue:80.0 / 255.0 alpha:1.0]; //设置删除视图 UIView *crossView = [self viewWithImageName:@"cross"]; UIColor *redColor = [UIColor colorWithRed:232.0 / 255.0 green:61.0 / 255.0 blue:14.0 / 255.0 alpha:1.0]; //设置定时提醒视图 UIView *clockView = [self viewWithImageName:@"clock"]; UIColor *yellowColor = [UIColor colorWithRed:254.0 / 255.0 green:217.0 / 255.0 blue:56.0 / 255.0 alpha:1.0]; //设置列表视图 UIView *listView = [self viewWithImageName:@"list"]; UIColor *brownColor = [UIColor colorWithRed:206.0 / 255.0 green:149.0 / 255.0 blue:98.0 / 255.0 alpha:1.0]; //设置默认的背景颜色 [cell setDefaultColor:_tableView.backgroundView.backgroundColor]; //设置单元格的代理 [cell setDelegate:self]; //设置每个单元格的实现效果 //第一个单元格 if (indexPath.row % kMCNumItems == 0) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Switch Mode Cell"]; [cell.detailTextLabel setText:@"Swipe to switch"]; //设置单元格的选中视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Checkmark\" cell"); }]; //设置单元格的删除视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Cross\" cell"); }]; //设置单元格的定时视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:clockView color:yellowColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState3 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Clock\" cell"); }]; //设置单元格的列表视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:listView color:brownColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState4 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"List\" cell"); }]; } //第二个单元格 else if (indexPath.row % kMCNumItems == 1) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Exit Mode Cell"]; [cell.detailTextLabel setText:@"Swipe to delete"]; //设置单元格的删除视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Cross\" cell"); [self deleteCell:cell]; }]; } //第三个单元格 else if (indexPath.row % kMCNumItems == 2) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Mixed Mode Cell"]; [cell.detailTextLabel setText:@"Swipe to switch or delete"]; //设置单元格的选中视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Checkmark\" cell"); }]; //设置单元格的删除视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Cross\" cell"); [self deleteCell:cell]; }]; } //第四个单元格 else if (indexPath.row % kMCNumItems == 3) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Un-animated Icons"]; [cell.detailTextLabel setText:@"Swipe"]; //设置图标是否移动 cell.shouldAnimateIcons = NO; //设置单元格的选中视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Checkmark\" cell"); }]; //设置单元格的删除视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Cross\" cell"); [self deleteCell:cell]; }]; } //第五个单元格 else if (indexPath.row % kMCNumItems == 4) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Right swipe only"]; [cell.detailTextLabel setText:@"Swipe"]; //设置单元格的定时视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:clockView color:yellowColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState3 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Clock\" cell"); }]; //设置单元格的列表视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:listView color:brownColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState4 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"List\" cell"); }]; } //第六个单元格 else if (indexPath.row % kMCNumItems == 5) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Small triggers"]; [cell.detailTextLabel setText:@"Using 10% and 50%"]; //设置第一个触发事件的距离百分比 cell.firstTrigger = 0.1; //设置第二个触发事件的距离百分比 cell.secondTrigger = 0.5; //设置单元格的选中视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Checkmark\" cell"); }]; //设置单元格的删除视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Cross\" cell"); [self deleteCell:cell]; }]; } //第七个单元格 else if (indexPath.row % kMCNumItems == 6) { //设置单元格的主标题、副标题 [cell.textLabel setText:@"Exit Mode Cell + Confirmation"]; [cell.detailTextLabel setText:@"Swipe to delete"]; //设置单元格的删除视图、颜色、状态、完成后的block [cell setSwipeGestureWithView:crossView color:redColor mode:MCSwipeTableViewCellModeExit state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { NSLog(@"Did swipe \"Cross\" cell"); //设置要删除的单元格 _cellToDelete = cell; //创建弾框 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"删除?" message:@"你确定要删除这个单元格?" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil]; [alertView show]; }]; }}
5.创建图片视图
//创建图片视图- (UIView *)viewWithImageName:(NSString *)imageName { UIImage *image = [UIImage imageNamed:imageName]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.contentMode = UIViewContentModeCenter; return imageView;}
6.UIAlertViewDelegate的实现
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //选择“否” if (buttonIndex == 0) { [_cellToDelete swipeToOriginWithCompletion:^{ NSLog(@"选择否"); }]; _cellToDelete = nil; } //选择"是“ else { //删除单元格 _nbItems--;//剩余单元格数 [_tableView deleteRowsAtIndexPaths:@[[_tableView indexPathForCell:_cellToDelete]] withRowAnimation:UITableViewRowAnimationFade]; }}
- (void)deleteCell:(MCSwipeTableViewCell *)cell { //删除单元格 _nbItems--;<span style="font-family: Arial, Helvetica, sans-serif;">//剩余单元格数</span> NSIndexPath *indexPath = [_tableView indexPathForCell:cell]; [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];}
- (IBAction)reload:(UIBarButtonItem *)sender { //重新加载单元格 _nbItems = kMCNumItems; //<span style="font-family: Arial, Helvetica, sans-serif;">kMCNumItems默认单元格数</span> [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];}
8.单元格的代理实现
//监听手指滑动的开始- (void)swipeTableViewCellDidStartSwiping:(MCSwipeTableViewCell *)cell { NSLog(@"开始滑动单元格!");}//监听手指滑动的结束- (void)swipeTableViewCellDidEndSwiping:(MCSwipeTableViewCell *)cell { NSLog(@"结束滑动单元格!");}//监听手指滑动的距离- (void)swipeTableViewCell:(MCSwipeTableViewCell *)cell didSwipeWithPercentage:(CGFloat)percentage { NSLog(@"已滑动百分比 : %.2f%%", percentage*100); }
二、MCSwipeTableViewCell的实现原理
1.MCSwipeTableViewCell.h文件
2.MCSwipeTableViewCell.m文件
0 0
- 【无限互联】框架学习——MCSwipeTableViewCell
- !软件开发的无限乐趣——学习ssh框架心得
- 连载一: 无限互联——我的学习生活
- 连载二:无限互联——我的学习生活
- 连载三:无限互联——我的学习生活
- 无限互联学习连载一
- 无限互联iOS学习连载一
- [无限互联]地图框架MapView(采用MKMapView)
- 【无限互联】浅析开源框架SDWebImage
- [无限互联] ios开发之框架HGPageScrollView浅谈
- 无限互联的日子——连载一
- 无限互联学习连载二 Block 循环引用
- 无限互联学习连载三 多线程单例
- 无限互联学习连载四 NSUserDefaults的使用
- 无限互联学习连载五 网络请求
- 无限互联学习连载六 SVN使用
- 无限互联学习连载七 数据库操作
- 我的无限互联学习记录(一)
- Matlab矩阵处理小结(4)-批量将矩阵保存到不同的文件--动态更新文件名
- 数字电路设计之Wallace树形乘法器
- 应用状态VS资源状态
- 数论基础之辗转相除法求最大公约数
- HTTP的可见性
- 【无限互联】框架学习——MCSwipeTableViewCell
- ACdream 1414(计算几何)
- INS-20802
- 专注于现在
- ROA的两个特性
- 统计大写字母个数——来自华为OJ平台测试基础篇
- Command Execution Source--medium
- cocos2d-x 3.2 2048——第六篇(最终章)
- 什么是资源