代码改变世界

IOS学习之路六(UITableView滑动删除指定行)

2013-08-25 13:09  Lves Li  阅读(653)  评论(0编辑  收藏  举报

滑动删除指定行代码如下:

 

Controller.h文件

 

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>  
  4. @property (nonatomic, strong) UITableView *myTableView;  
  5. @property(nonatomic,strong) NSMutableArray *arrayOfRows;  
  6. @end  



 

Controller.m文件

 

  1. //  
  2. //  TableViewController.m  
  3. //  UITableViewDemo  
  4. //  
  5. //  Created by WildCat on 13-8-6.  
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.  
  7. //  
  8.   
  9. #import "TableViewController.h"  
  10.   
  11. @interface TableViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation TableViewController  
  16. @synthesize myTableView;  
  17. @synthesize arrayOfRows;  
  18.   
  19.   
  20. - (void)viewDidLoad  
  21. {  
  22.     [super viewDidLoad];  
  23.     // Do any additional setup after loading the view, typically from a nib.  
  24.     self.view.backgroundColor = [UIColor whiteColor];  
  25.     myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];  
  26.     //设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式  
  27.     self.myTableView.delegate = self;//设置代理为自身  
  28.     self.myTableView.dataSource = self;//设置数据源为自身  
  29.     self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;  
  30.     //确保TablView能够正确的调整大小  
  31.     arrayOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d", nil];//初始化表格数据  
  32.     [self.view addSubview:myTableView];  
  33.       
  34. }  
  35. //设置每行的高度  
  36. -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  37.     CGFloat result = 20.0f;  
  38.     if ([tableView isEqual:self.myTableView]) {  
  39.          
  40.         result = 80.0f;  
  41.     }  
  42.     return result;  
  43. }  
  44. //允许数据源告知必须加载到Table View中的表的Section数。  
  45. //-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  46. //    NSInteger result = 0;  
  47. //    if([tableView isEqual:myTableView]){  
  48. //        result = 3;//一共三个section  
  49. //    }  
  50. //    return result;  
  51. //}  
  52. //设置每个Section呈现多少行  
  53. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  54.     return [self.arrayOfRows count];  
  55. }  
  56. //每行对应的数据  
  57. -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  58.     UITableViewCell *result = nil;  
  59.     if ([tableView isEqual:myTableView]) {  
  60.         static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识  
  61.         result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象  
  62.         if (result == nil) {  
  63.             result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。  
  64.         }  
  65.         //indexPath.section 表示section的索引 indexPath.row表示行数的索引  
  66.         result.textLabel.text = [self.arrayOfRows objectAtIndex:indexPath.row];  
  67.     }  
  68.     return result;  
  69. }  
  70.   
  71. //点击某一行时候触发的事件  
  72. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  73.     if ([tableView isEqual:myTableView]) {  
  74.         NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);  
  75.     }  
  76. }  
  77. //要求委托方的编辑风格在表视图的一个特定的位置。  
  78. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{  
  79.     UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格  
  80.     if ([tableView isEqual:myTableView]) {  
  81.         result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格  
  82.     }  
  83.     return result;  
  84. }  
  85.   
  86. -(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。  
  87.     [super setEditing:editing animated:animated];  
  88.     [self.myTableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。  
  89. }  
  90.   
  91. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。  
  92.     if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式  
  93.         if (indexPath.row<[self.arrayOfRows count]) {  
  94.             [self.arrayOfRows removeObjectAtIndex:indexPath.row];//移除数据源的数据  
  95.             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据  
  96.         }  
  97.     }  
  98. }  
  99.   
  100.   
  101. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  102. {  
  103.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  104.     if (self) {  
  105.         // Custom initialization  
  106.     }  
  107.     return self;  
  108. }  
  109.   
  110.   
  111.   
  112. - (void)viewDidUnload  
  113. {  
  114.     [super viewDidUnload];  
  115.     // Release any retained subviews of the main view.  
  116.    // self.myTableView = nil;  
  117. }  
  118.   
  119. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  120. {  
  121.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  122. }  
  123.   
  124. @end  


执行截图:

 

 

 转载请注明:

新浪微博:http://weibo.com/u/3202802157