当前位置:首页 >探索 >iOS之UITableView重新排序 新排序 表格视图在ios开发中

iOS之UITableView重新排序 新排序 表格视图在ios开发中

2024-07-01 10:41:46 [百科] 来源:避面尹邢网

iOS之UITableView重新排序

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

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

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

iOS之UITableView重新排序 新排序 表格视图在ios开发中

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

iOS之UITableView重新排序 新排序 表格视图在ios开发中

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

sample code:

iOS之UITableView重新排序 新排序 表格视图在ios开发中

.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 iOS开发UITableView重新排序

(责任编辑:焦点)

    推荐文章
    • 九兴控股(01836.HK)发布公告:授出1969.5万份购股权

      九兴控股(01836.HK)发布公告:授出1969.5万份购股权九兴控股(01836.HK)发布公告,2021年3月18日,公司根据采纳的购股权计划向承授人授出合共1969.5万份购股权,惟须待承授人接纳后,方可作实。所授出购股权的认购价9.54港元,购股权自授出 ...[详细]
    • V观财报|赣锋锂业2023年净利预降70%

      V观财报|赣锋锂业2023年净利预降70%中新经纬1月30日电 赣锋锂业2023年业绩大幅预降。30日盘后,赣锋锂业发布2023年业绩预告,预计归属上市公司股东的净利润42亿元-62亿元,同比降79.52%-69.76%;扣除非经常性损益后的 ...[详细]
    • 农家乐暗藏“百家乐”!绵阳涪城警方捣毁一聚众赌博窝点,5人被刑拘

      农家乐暗藏“百家乐”!绵阳涪城警方捣毁一聚众赌博窝点,5人被刑拘1月30日,四川绵阳市公安局涪城区分局发布消息称,近日,该局捣毁一个聚众赌博窝点,现场传唤涉赌人员15人,缴获赌资8万余元。经查,犯罪嫌疑人陈某、黄某等人,在一偏僻的农家乐内,利用网络连接境外赌博网站 ...[详细]
    • 城市绿地与学龄儿童视力水平呈正相关

      城市绿地与学龄儿童视力水平呈正相关本报讯记者朱汉斌)近日,中山大学公共卫生学院教授陈亚军课题组研究发现,城市绿地与学龄儿童视力水平呈正相关,与视力受损风险呈负相关。相关成果发表于《环境国际》。“该研究是一项以中国大样本学龄儿童人群为对 ...[详细]
    • 十部门:大力推进重点行业清洁低碳改造 加快淘汰落后产能

      十部门:大力推进重点行业清洁低碳改造 加快淘汰落后产能11月9日,发改委等十部门联合印发《“十四五”全国清洁生产推行方案》,方案提出,大力推进重点行业清洁低碳改造。严格执行质量、环保、能耗、安全等法律法规标准,加快淘汰落后产能。全 ...[详细]
    • vivo总裁沈炜新年致辞:前行即答案

      vivo总裁沈炜新年致辞:前行即答案1月30日,vivo在广东东莞全球总部召开2023线上年会暨创新颁奖盛典。vivo创始人、总裁兼首席执行官沈炜在会上发表题为《前行即答案》的演讲,总结vivo近一年经营成果,诠释科技创新与经营管理理念 ...[详细]
    • 广州开出春运首趟爱心专列

      广州开出春运首趟爱心专列免费送老乡回家 新快报讯 记者许力夫 通讯员 黄惠萍 胡靖报道 “我们要回家过年咯!”1月30日7时4分,载有320名贵州安顺籍在粤务工人员的D1846次列车驶离广州南站,一路向西驰往贵州。 据铁路部 ...[详细]
    • 2023年我国十种常用有色金属产量首超7000万吨

      2023年我国十种常用有色金属产量首超7000万吨据新华社电 2023年,我国有色金属工业稳中向好的态势日趋明显。初步统计,十种常用有色金属产量为7469.8万吨,首次突破7000万吨,按可比口径计算比上年增长7.1%。这是中国有色金属工业协会副会长 ...[详细]
    • 花呗为什么提前还款是大忌 具体原因有哪些?

      花呗为什么提前还款是大忌 具体原因有哪些?很多人会使用花呗提前消费,无法一次性还款就会办理花呗分期,等手里头有钱了就打算提前还款。虽说花呗分期是支持提前还款,可有不少人认为花呗提前还款是大忌。那么,花呗为什么提前还款是大忌?这里就来给大家分析 ...[详细]
    • 我国科学家在全球科技治理中影响力不断提升

      我国科学家在全球科技治理中影响力不断提升1月30日,中国科协第十届全国委员会第八次会议在北京召开。中国科协主席万钢在工作报告中指出,2023年我国科学家在全球科技治理中的影响力不断提升,国际科技界“朋友圈”越来越大。“我国科学家不断走上国际 ...[详细]
    热点阅读