导航控制器和表视图(二)

发布时间:2020-06-20 14:06:18 作者:新风作浪
来源:网络 阅读:418
      本文是在 导航控制器和表视图(一) 添加代码,实现的和

UITableViewCell的标记、移动、删除、插入,不同的是把他们分别放在不同单元格视图中,只要就是委托方法的使用,此处不在分析全部代码,但提供全部代码下载;

先把效果图贴上:

导航控制器和表视图(二)  导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)导航控制器和表视图(二)

从中看出每个视图都有一个cell单元格,也就对应每个控制器都有一个数组,删除功能的存放在了plist文件中,他们的父类都是SecondLevelViewController  拿标记功能的模块举例
1.表视图标记
#import <UIKit/UIKit.h> #import "SecondLevelViewController.h" @interface CheckListController : SecondLevelViewController @property (nonatomic,strong) NSArray *list; @property (nonatomic,strong) NSIndexPath *lastIndexPath;// @end

-(void)viewDidLoad {     NSArray *array = [[NSArray alloc] initWithObjects:@"Who has",@"BUB gruop",@"Who Pudding",@"Scodey Snack",@"EverLasting",@"Green Eggs annd",@"Soy Lent",@"Hard Tack",@"Lembas",@"Roast Breaf",@"Lembas Bread",@"Roast Beast",@"Bancmangr", nil];     self.list = array;     [super viewDidLoad]; }

在设置单元格标记的时候需要注意的两个委托方法,一个是创建cell的时候,另一个是我们在标记时候发生的改变状况
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentfier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];     if (cell==nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier];     } //    从单元视图中提取行     NSUInteger row = [indexPath row]; //    从当前选项提取行     NSUInteger oldRow = [self.lastIndexPath row]; //    从数组中获取一行值,将他分给单元格的标题     cell.textLabel.text = [self.list objectAtIndex:row]; //    判断两个行是否是一行,如果是则cell的accessory属性为UITableViewCellAccessoryCheckmark,否则设置为UITableViewCellAccessoryNone,还要确保lastIndexPath不为空     cell.accessoryType =(row == oldRow && self.lastIndexPath !=nil)? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;     return cell; }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //    从单元视图中提取行     int newRow = [indexPath row]; //    如果获取上次选中行不为空,则提取上次选中的行     int oldRow = (self.lastIndexPath != nil)?[self.lastIndexPath row] : -1; //    如果从视图中选取当前行和上次在视图中选取的不一样     if (newRow != oldRow) { //        获取刚才选中单元知道标记作为拓展图标         UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];         newCell.accessoryType = UITableViewCellAccessoryCheckmark; //        把上次选中的单元的拓展图标设置为无         UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];         oldCell.accessoryType = UITableViewCellAccessoryNone; //        储存本次标记行索引,下次标记时使用         self.lastIndexPath=indexPath;         } //    当我们单元表格视图拓展标记改变之后就要把现在标记的单元格设置为选中状态     [tableView deselectRowAtIndexPath:indexPath animated:YES];      }

在FirstLevelViewController.m中viewDidLoad函数里添加
 /******** Check List *********/     CheckListController *checkListController = [[CheckListController alloc] initWithStyle:UITableViewStylePlain];     checkListController.title = @"Check One";     checkListController.rowImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"]; //    添加控制器对象到数组中,显示在导航控制器一层     [array addObject:checkListController];

2.表视图行上添加按钮
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *RowControlIdentfier = @"Row ControlIdentfier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RowControlIdentfier];     if (cell == nil) {         cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RowControlIdentfier];         UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];         UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"]; //        由于UIButton的buttonType属性被声明为只读,因此需要用buttonWithType来创建按钮,不能使用alloc init 来创建,因为不能将按钮的类型更改为UIButtonTypeCustom,此操作目的是自定义按钮图像         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //        设置按钮大小与图像匹配         button.frame = CGRectMake(0, 0, buttonUpImage.size.width, buttonUpImage.size.height); //        设置正常状态下按钮背景图片         [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; //        设置按钮被按下高亮状态下的背景图片         [button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];         [button setTitle:@"Tap" forState:UIControlStateNormal];         [button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside]; //        把定义的button分配给单元的附加视图         cell.accessoryView = button;     }     NSUInteger row = [indexPath row];     NSString *rowTitle = [self.list objectAtIndex:row];     cell.textLabel.text=rowTitle;     return cell; }

3.表视图的行上添加按钮
-(void)viewDidLoad {     if (self.list==nil) {         NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"Eeny",@"Meey",@"Miney",@"Miney",@"Moe",@"Toe",@"Error",@"HELAN sdfh",@"AKEB",@"By",@"For", nil];         self.list=array;     } //    在导航栏上添加按钮,并添加一个触发事件     UIBarButtonItem *moveButon = [[UIBarButtonItem alloc]initWithTitle:@"Move"                                                                   style:UIBarButtonItemStyleBordered target:self                                                                 action:@selector(toggleMove)]; //    设置按钮为导航栏上又按钮     self.navigationItem.rightBarButtonItem = moveButon;     [super viewDidLoad]; }

//点击导航栏右边按调用这个方法,使单元格变成可编辑模式 -(void)toggleMove {     [self.tableView setEditing:!self.tableView.editing animated:YES];     if (self.tableView.editing)          [self.navigationItem.rightBarButtonItem setTitle:@"Done"];     else          [self.navigationItem.rightBarButtonItem setTitle:@"Move"]; }

//通过此方法,表视图询问指定行是否可以被删除,是否可以新行插入 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { //    返回值表示不可删除和新行插入操作     return UITableViewCellEditingStyleNone; }

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //  检索需要移动的行     NSUInteger fromRow = [sourceIndexPath row]; //   检索行新位置     NSUInteger toRow = [destinationIndexPath row]; //    下面的从数组中移除指定对象 //    检索一个指向要移动的对象的指针,并保留对象,这样在数组中删除对象时候对象不会被释放     id object = [self.list objectAtIndex:fromRow];     [self.list removeObjectAtIndex:fromRow]; //    移除之后插入指定新位置     [self.list insertObject:object atIndex:toRow]; }

4.删除行
从属性列表plist中加载的数组,
-(void)viewDidLoad {     if (self.list==nil) {         NSString *path = [[NSBundle mainBundle] pathForResource:@"computers" ofType:@"plist"];         NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];         self.list = array;     }     UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Delete"                                                                   style:UIBarButtonItemStyleBordered target:self                                                                  action:@selector(toggleEdit:)];     self.navigationItem.rightBarButtonItem = editButton;     [super viewDidLoad];      }

#pragma mark - #pragma mark Table View Data Source Methods  -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //    获取当前正处于编辑状态下的行索引     NSUInteger row =[indexPath row]; //    从属性列表中删除该对象     [self.list removeObjectAtIndex:row]; //    通知tabView表删除该行     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }


附上源代码:http://download.csdn.net/detail/duxinfeng2010/4432336


推荐阅读:
  1. 列表视图ListView之二
  2. NVisionXR_iOS教程二 —— 创建控制器和Vuforia对象

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

表视图 导航控制器 表视

上一篇:MySQL主从同步常见报错的解决办法2

下一篇:面试必会之HashMap源码分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》