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

      本文是在 导航控制器和表视图(一) 添加代码,实现的和

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值