UITableView 实例详解 滑动编辑 headerView

转自:http://blog.csdn.net/reylen/article/details/8505960


[cpp]
 view plain copy
 
 print?
  1. self.dataArray = [[[NSMutableArray alloc]init] autorelease];  
  2. NSArray *array = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Beijing",@"$500", nil],[NSArray arrayWithObjects:@"ShangHai",@"$510", nil],[NSArray arrayWithObjects:@"Guangzhou",@"$540", nil],[NSArray arrayWithObjects:@"Wuhan",@"$513", nil], nil];  
  3. [dataArray addObjectsFromArray:array];  
  4.   
  5. int height = [UIScreen mainScreen].applicationFrame.size.height;  
  6. self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, height - 40) style:UITableViewStylePlain];  
  7. self.tableView.dataSource = self;  
  8. self.tableView.delegate = self;  
  9. [self.view addSubview:self.tableView];  

点击 home button,进入编辑

                           

纵向滑动tableCell 出现编辑按钮

[cpp] view plain copy
 
 print?
  1. #pragma mark - UITableViewDataSource/Delegate  
  2. //返回table 行数  
  3. - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  4.     return dataArray.count;  
  5. }  
  6.   
  7. - (UITableViewCell*) tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  8.     static NSString *identifier = @"cell";  
  9.     UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier];  
  10.     if (cell == nil) {  
  11.         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];  
  12.     }  
  13.       
  14.     NSArray *data = [dataArray objectAtIndex:indexPath.row];  
  15.       
  16.     cell.textLabel.text = [data objectAtIndex:0];  
  17.     cell.detailTextLabel.text = [data objectAtIndex:1];  
  18.     cell.detailTextLabel.textColor = [UIColor redColor];  
  19.       
  20.     return cell;  
  21.       
  22. }  
  23. //- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {  
  24. //    NSString *headerString = @"Address                                   Price";  
  25. //    return headerString;  
  26. //}  
  27. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {  
  28.     return @"删除";  
  29. }  
  30. //纵向滑动时响应,出现编辑按钮  
  31. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{  
  32.       
  33.     NSLog(@"Start editing!");  
  34.       
  35. }  
  36. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath  
  37. {        //纵向滑动时出现编辑按钮  
  38.     return YES;  
  39. }  
  40.   
  41. //让行可以移动  
  42. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  
  43. {  
  44.     return YES;  
  45. }  
  46. - (void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {      //编辑完响应事件  
  47.     UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];  
  48.     NSLog(@"title:%@",cell.textLabel.text);  
  49.     NSLog(@"price:%@",cell.detailTextLabel.text);  
  50.       
  51.     [dataArray removeObjectAtIndex:indexPath.row];  
  52.     [tableView reloadData];  
  53.       
  54. }  
  55. //设置table headerView  
  56. - (UIView*) tableView:(UITableView *)_tableView viewForHeaderInSection:(NSInteger)section {  
  57.     UIView *headerView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 20)] autorelease];  
  58.     UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 20)];  
  59.     leftLabel.textColor = [UIColor orangeColor];  
  60.     UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(_tableView.frame.size.width - 100, 0, 90, 20)];  
  61.     rightLabel.textColor = [UIColor orangeColor];  
  62.       
  63.     leftLabel.text = @"Address";  
  64.     rightLabel.text = @"Price";  
  65.     rightLabel.textAlignment = NSTextAlignmentRight;  
  66.     leftLabel.backgroundColor = [UIColor clearColor];  
  67.     rightLabel.backgroundColor = [UIColor clearColor];  
  68.       
  69.     UIImageView *backImage = [[UIImageView alloc]initWithFrame:headerView.frame];  
  70.     backImage.image = [UIImage imageNamed:@"back.png"];  
  71.       
  72.     [headerView addSubview:backImage];  
  73.       
  74.     [headerView addSubview:leftLabel];  
  75.     [headerView addSubview:rightLabel];  
  76.       
  77.     [leftLabel release];  
  78.     [rightLabel release];  
  79.     [backImage release];  
  80.       
  81.     return headerView;  
  82. }  


 
 
原文地址:https://www.cnblogs.com/feiyu-mdm/p/5570398.html