8-22学习练习[一个viewController整合增删移动功能]

简介: 一个tableView页面,左上角的按钮用来添加行,同时可以移动行,右上角的按钮用来删除行,不能移动行,要求第一行不能被改变(删除,移动,增加),第一行用来返回上一级。

一个tableView页面,左上角的按钮用来添加行,同时可以移动行,右上角的按钮用来删除行,不能移动行,要求第一行不能被改变(删除,移动,增加),第一行用来返回上一级。

实现效果:


上面展示了一个从根视图跳转到子视图,再从添加选项到移动再到删除,然后返回根视图的一个过程。

操作步骤:

1.创建一个Empty项目

2.创建根视图控制器,命名为FirstViewController

FirstViewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UITableViewController
@property(nonatomic,retain) NSMutableArray *array;
@end

FirstViewController.m:

#import "FirstViewController.h"
#import "DXWViewController.h"
@interface FirstViewController ()

@end
NSString *CellIdentifier = @"Cell";

@implementation FirstViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.array = @[
                       [[DXWViewController alloc] initWithStyle:UITableViewStylePlain]];
        self.title = @"目录列表";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    int row = [indexPath row];
    cell.textLabel.text = ((DXWViewController *)self.array[row]).title;
    //有右边的>符号
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row];
    [self.navigationController pushViewController:self.array[row] animated:YES];
}


3.修改AppDelegate

#import "DXWAppDelegate.h"
#import "FirstViewController.h"
@implementation DXWAppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    //创建首页
    FirstViewController *first = [[FirstViewController alloc] initWithStyle:UITableViewStylePlain];
    //创建一个导航栏,其中的首页是FirstViewController
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];
    //将导航栏作为根视图控制器
    self.window.rootViewController  = nav;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}


4.创建子视图控制器,命名为DXWViewController

DXWViewController.h:

#import <UIKit/UIKit.h>

@interface DXWViewController : UITableViewController<UIAlertViewDelegate>
@property(nonatomic,retain)NSMutableArray *array;
@property(retain,nonatomic)NSIndexPath *indexPath;
@end

DXWViewController.m:

#import "DXWViewController.h"
BOOL RIGHTBUTTON;
@interface DXWViewController ()

@end

@implementation DXWViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {
        self.title = @"子视图";
        NSArray *arr = @[@"返回上一级",@"小花",@"小李",@"小王",@"小丁",@"小张",@"小康",@"小刘",@"小墩",@"大墩"];
        self.array = [arr mutableCopy];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleBordered target:self action:@selector(itemLeftButtonClick:)];
	self.navigationItem.leftBarButtonItem = button;
    
    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(itemRightButtonClick:)];
    self.navigationItem.rightBarButtonItem = button1;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    int row = [indexPath row];
    cell.textLabel.text = self.array[row];
    return cell;
}

-(void)itemLeftButtonClick:(id)sender
{
    
    //设置可以编辑
    RIGHTBUTTON = false;
    [self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态
    if (self.tableView.editing)
    {
        [self.navigationItem.leftBarButtonItem setTitle:@"完成"];
    }
    else
    {
        [self.navigationItem.leftBarButtonItem setTitle:@"添加"];
    }
}

-(void)itemRightButtonClick:(id)sender
{
    RIGHTBUTTON = true;
    //设置可以编辑
    [self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态
    if (self.tableView.editing)
    {
        [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
    }
    else
    {
        [self.navigationItem.rightBarButtonItem setTitle:@"删除"];
    }
}

//设置编译图标
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除
    if (RIGHTBUTTON)
    {
        return UITableViewCellEditingStyleDelete;
    }
    else
    {
        return UITableViewCellEditingStyleInsert;
        //return UITableViewCellEditingStyleNone;
    }
}
//设置是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (RIGHTBUTTON)
    {
        return NO;
    }
    else
    {
        if (indexPath != 0) {
            return YES;
        }
    }
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0) {
        return NO;
    }
    else
    {
        return YES;
    }
}

//实现UIAlertView代理协议
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    //获取AlertView中的数据
    NSString *str = [alertView textFieldAtIndex:0].text;
    //将数据插入到array数组中(位置是点击的位置的下一行)
    [self.array insertObject:str atIndex:[self.indexPath row]+1];
    //用点击位置的下一行创建一个新的NSIndexPath
    int newrow = [self.indexPath row]+1;
    NSIndexPath *index = [NSIndexPath indexPathForRow:newrow inSection:0];
    //在tableView的这个NSIndexPath中插入数据
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation: UITableViewRowAnimationFade];
    
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除按钮
    if (RIGHTBUTTON) {
        if ([indexPath row] != 0)
        {
            int row = [indexPath row];
            [self.array removeObjectAtIndex:row];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        }
    }
    //添加按钮
    else
    {
        //获得点击的indexPath
        self.indexPath = indexPath;
        //创建一个UIAlertView,用来获得数据
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"enterstring" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
        //带有输入框的UIAlertView
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
    }
}


//可以根据行来设置delete按钮位置上button的标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"你删我一个试试";
}

-(void)dealloc
{
    [self.array release];
    [self.indexPath release];
    [super dealloc];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    //蓝色背景消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
    
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (!RIGHTBUTTON) {
        if ([destinationIndexPath row]!=0) {
            id object= self.array[[sourceIndexPath row]];
            [self.array removeObject:self.array[[sourceIndexPath row]]];
            [self.array insertObject:object atIndex:[destinationIndexPath row]];
            NSLog(@"%d",[self.array count]);
            NSLog(@"当前位置%d,目标位置%d",[sourceIndexPath row],[destinationIndexPath row]);
        }
        //如果是跟第一行移动,则不允许这样操作,重新加载数据
        [self.tableView reloadData];
    }
}
@end

Demo源文件:

http://download.csdn.net/detail/s10141303/5995485


相关文章
|
9月前
|
设计模式 PHP
php设计模式--观察者模式(4.1)面向过程完成页面内容切换
php设计模式--观察者模式(4.1)面向过程完成页面内容切换
54 0
|
9月前
|
程序员 iOS开发
页面的循环引用与监听事件的添加与删除,页面逻辑管理
页面的循环引用与监听事件的添加与删除,页面逻辑管理
48 0
|
9月前
|
Oracle Java 关系型数据库
java实现遍历树形菜单方法——数据库表的创建
java实现遍历树形菜单方法——数据库表的创建
|
9月前
|
C++ 容器
【C++11特性篇】一文带小白轻松理解【万能引用(引用折叠)】&【完美转发】
【C++11特性篇】一文带小白轻松理解【万能引用(引用折叠)】&【完美转发】
|
Android开发
Android开发 ListView(垂直滚动列表项视图)的简单使用
Android开发 ListView(垂直滚动列表项视图)的简单使用
372 0
Android开发 ListView(垂直滚动列表项视图)的简单使用
【小试身手】几个自定义控件的组合应用,实现简单的“增删改查”功能(有源码)
     分页控件、查询控件、显示数据的控件和表单控件,终于把这几个控件结合在一起了,和在一起之后就可以让“增删改查”变的非常的简单和容易了,当然还需要数据访问函数库的支持。      综合演示的IDE是 vs2008,.net Framework2.0,C#, B/S 。
1054 0
|
Android开发 Python 数据格式
界面无小事(八):RecyclerView增删item
界面无小事(一):RecyclerView+CardView了解一下界面无小事(二):让RecyclerView展示更多不同视图界面无小事(三):用RecyclerView + Toolbar做个文件选择器界面无小事(四):来写个滚动选择器吧!界面无小...
1223 0
|
Android开发 Java 数据格式
界面无小事(七):使用代码动态增删布局
界面无小事(一):RecyclerView+CardView了解一下界面无小事(二):让RecyclerView展示更多不同视图界面无小事(三):用RecyclerView + Toolbar做个文件选择器界面无小事(四):来写个滚动选择器吧!界面无小...
1357 0
|
前端开发 容器
用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机
原文:用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机       在项目开发中经常会遇到这样一种情况,就是需要将用户填写的信息排版到一张表单中,供打印或存档。
843 0
|
Android开发 Java 数据格式
使用代码动态增删布局(2018.8重编版)
目录 效果图 前言 布局文件 实现 最后 效果图 不多废话, 先上图, 有兴趣再看下去: 效果图 前言 用代码增删布局还是很常用的.
1039 0