iOS之UITableView重新排序

移动开发 iOS
我们可以使用两种方式使用表格,第一种是直接使用UITableViewController,该类是UIViewController的子类;第二种 我们可以使用在UIViewController的view视图中添加UITableView,再继承UITableViewDataSource和 UITableViewDelegate 协议。

表格视图在ios开发中,经常使用到的视图,几乎每个app 中多多少少都会有UITableView的影子,就是因为UITableView的功能非常强大,使用起来也非常简单,苹果公司也对接口做了很好的封装, 才使用ios程序员这么喜欢它。使用表格视图相关的类 UITableViewController,UITableView,UITableViewDataSource,UITableViewDelegate.

我们可以使用两种方式使用表格,第一种是直接使用UITableViewController,该类是UIViewController的子类;第二种 我们可以使用在UIViewController的view视图中添加UITableView,再继承UITableViewDataSource和 UITableViewDelegate 协议。

在这里我实现UITableView重新排序,主要使用到UITableDelegate中的两个方法:

  1. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; 
  2.     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath; 

sample code:

.h文件

  1. #import <UIKit/UIKit.h> 
  2.     @interface ReorderViewController : UITableViewController 
  3.     @end 

 .m文件

  1. // 
  2.     //  ReorderViewController.m 
  3.     //  ReorderTable 
  4.     // 
  5.     //  Created by Carl on 13-6-5. 
  6.     //  Copyright (c) 2013年 Carl. All rights reserved. 
  7.     //    
  8.     #import "ReorderViewController.h" 
  9.     @interface ReorderViewController () 
  10.     @property (nonatomic,strong) NSMutableArray * dataSource; 
  11.     @end 
  12.     @implementation ReorderViewController     
  13.     - (id)initWithStyle:(UITableViewStyle)style 
  14.     { 
  15.         self = [super initWithStyle:style]; 
  16.         if (self) { 
  17.             // Custom initialization 
  18.         } 
  19.         return self; 
  20.     }     
  21.     - (void)viewDidLoad 
  22.     { 
  23.         [super viewDidLoad]; 
  24.         // Uncomment the following line to preserve selection between presentations. 
  25.         // self.clearsSelectionOnViewWillAppear = NO;      
  26.         // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
  27.         self.navigationItem.rightBarButtonItem = self.editButtonItem; 
  28.         self.title = @"Recordering Rows"
  29.         self.dataSource = [[NSMutableArray alloc] initWithObjects:@"Drag to reorder 1 ",@"Drag to reorder 2 ",@"Drag to reorder 3 ",@"Drag to reorder 4 ",@"Drag to reorder 5 ",@"Drag to reorder 6 ", nil]; 
  30.     //    self.editing = YES; 
  31.     } 
  32.     - (void)didReceiveMemoryWarning 
  33.     { 
  34.         [super didReceiveMemoryWarning]; 
  35.         // Dispose of any resources that can be recreated. 
  36.     } 
  37.       
  38.     #pragma mark - Table view data source 
  39.     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
  40.     { 
  41.         // Return the number of sections. 
  42.         return 1; 
  43.     } 
  44.     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  45.     { 
  46.         // Return the number of rows in the section. 
  47.         return [_dataSource count]; 
  48.     } 
  49.     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  50.     { 
  51.         static NSString *CellIdentifier = @"Cell"
  52.         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
  53.         if(cell == nil) 
  54.         { 
  55.             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
  56.         } 
  57.         cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row]; 
  58.         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
  59.         // Configure the cell... 
  60.         return cell; 
  61.     }     
  62.     /* 
  63.     -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
  64.     { 
  65.         return UITableViewCellEditingStyleNone; 
  66.     } 
  67.     -(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
  68.     { 
  69.         return NO; 
  70.     }
  1. <span style="font-size:9pt;line-height:1.5;">*/</span>
  1. // Override to support rearranging the table view. 
  2.     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
  3.     { 
  4.         [_dataSource exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; 
  5.     } 
  6.     // Override to support conditional rearranging of the table view. 
  7.     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
  8.     { 
  9.         // Return NO if you do not want the item to be re-orderable. 
  10.         return YES; 
  11.     } 
  12.     #pragma mark - Table view delegate 
  13.     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
  14.     { 
  15.         // Navigation logic may go here. Create and push another view controller. 
  16.         /* 
  17.          <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
  18.          // ... 
  19.          // Pass the selected object to the new view controller. 
  20.          [self.navigationController pushViewController:detailViewController animated:YES]; 
  21.          */ 
  22.     } 
  23.     @end 

效果图:

  如果想在edit状态取消delete按钮,需要实现以下两个方法:

  1. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
  2.     { 
  3.         return UITableViewCellEditingStyleNone; 
  4.     } 
  5.     -(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
  6.     { 
  7.         return NO; 
  8.     } 

如果想一装入视图就显示move按钮,需要在viewDidLoad中添加以下代码

  1. self.editing = YES; 

效果图:

责任编辑:闫佳明 来源: oschina
相关推荐

2011-07-07 16:38:21

iOS UITableVie

2013-07-25 14:12:53

iOS开发学习UITableView

2012-04-04 22:36:52

iOS5

2009-12-07 13:50:10

PHP函数shuffl

2013-06-20 11:10:24

iOS开发UItableView单元格背景渐变

2017-07-27 20:21:06

iOSUITableView富文本编辑器

2022-11-21 07:58:10

Java排序冒泡排序

2015-10-20 15:09:55

排序算法

2013-07-18 18:14:26

UITableViewiOS长按手势UILongPress

2021-01-19 07:02:26

算法数据结构堆排序

2011-11-17 10:08:12

iOSGmail

2013-09-16 10:14:35

iOS7App Store

2019-06-20 08:13:33

物联网IOT技术

2017-02-24 09:30:17

iOS签名代码

2011-08-02 17:14:41

iPhone应用 UITableVie

2016-12-13 15:41:40

JavaHashMap

2014-07-23 13:17:53

iOSUITextField

2014-07-21 14:49:35

iOSUILabel

2011-04-20 11:22:51

Java

2011-07-27 11:19:33

iPhone UITableVie
点赞
收藏

51CTO技术栈公众号