这次的学习是在Navigation-based Application模板中,用RootViewController class设置操作方法,使用UITableView的属性值。在导航控制器控件为程序的窗口添加上导航条,可构建多个视图连接导航按钮。这次的练习中,我在Navigation controller控件加入两个导航按钮,屏幕左上角Add按钮为表格添加新的一行,右上角Edit按钮为表格删除一行或者移动每行的顺序。当user点击edit按钮后,便会进入到编辑的视图,当user想要回到原先的视图便点击Done完成编辑。

 
 
编码:
 

ViewController.m文件

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

{

IBOutlet UITableView *rootTableView;

IBOutlet UIButton *editButton;

NSMutableArray *dataArray;

UITextField *rowField;

}

@property (nonatomic,retain) NSMutableArray *dataArray;

@property (nonatomic, retain) UITextField *rowField;

@property (nonatomic, retain) IBOutlet UITableView *rootTableView;

@property (nonatomic, retain) IBOutlet UIButton *editButton;

@end

ViewController.h文件

 

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize rootTableView;

@synthesize dataArray;

@synthesize editButton;

@synthesize rowField;

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//定义内容文字

dataArray = [[NSMutableArray alloc] initWithObjects:@"Row1", @"Row2",@"Row3",@"Row4",@"Row5",nil];

//建立add按钮

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]

initWithTitle:@"Add"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(AddButtonAction:)];

self.navigationItem.leftBarButtonItem = addButton;

}

//构建操作方法,点击按钮执行

-(IBAction) EditButtonAction:(id)sender

{

//    [rootTableView setEditing: YES

//                     animated: YES];

if ([sender tag] == 1) {

[editButton setTitle:@"Done" forState:UIControlStateNormal];

[editButton setTag:2];

[rootTableView setEditing:YES animated:YES];

}else if ([sender tag] == 2){

[editButton setTitle:@"Edit" forState:UIControlStateNormal];

[editButton setTag:1];

[rootTableView setEditing:NO animated:YES];

}

}

//添加addbutton

-(IBAction)AddButtonAction:(id)sender

{

UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"添加一行"

message:@""

delegate:self

cancelButtonTitle:@"取消"

otherButtonTitles:@"确定", nil];

rowField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 38.0, 245.0, 20.0)];

[rowField setBackgroundColor:[UIColor whiteColor]];

[dialog addSubview:rowField];

[dialog show];

[dialog release];

[rowField release];

}

-(void) alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

if ((buttonIndex != [alertView cancelButtonIndex]) && (rowField.text != nil)) {

[dataArray insertObject:[rowField text] atIndex:dataArray.count];

[self.rootTableView reloadData];

}

}

//表格中分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2;

}

//表格中行数

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

{

//表格中行数等于数组内容的个数

if (section ==0) {

return dataArray.count;

}

if (section ==1)

{

return 0;

}

else {

return 0;

}

}

//分组标题内容设置

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

//创建字符变量title

NSString *title = nil;

switch (section) {

case 0:

title = @"表格一";

break;

case 1:

title = @"表格二";

break;

default:

break;

}

return title;

}

//显示表格每一行内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

//创建一个字符变量,用于取得文本数据类型

static NSString *CellIndentifier = @"cell";

//建立表格行数单元格

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];

//当cell为空时

if (cell == nil)

{

//为cell重新获取表格内容标识符

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];

}

//分组section为0

if (indexPath.section ==0)

{

cell.textLabel.text =

[dataArray objectAtIndex:indexPath.row];

}

return cell;

}

//编辑

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

//调整

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete)

{

//删除

[dataArray removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]

withRowAnimation:UITableViewRowAnimationFade];

}

}

//上下行移动

-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

[dataArray insertObject:[dataArray objectAtIndex:sourceIndexPath.row]

atIndex:destinationIndexPath.row];

[dataArray removeObjectAtIndex:(NSUInteger) sourceIndexPath.row+1];

}

//让表格内容位置调整的方法

-(BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view.

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

-(void) dealloc

{

[rootTableView release];

rootTableView = nil;

[rowField release];

[dataArray release];

dataArray = nil;

[editButton release];

editButton = nil;

[super dealloc];

}

@end

 
保存文件,打开ViewController.xib文件, 在view中添加一个Table view控件,
将delegate和dataSource连接到File's Owner图标。
接着添加两个Button控件,将add和edit连接到File's owner图标上,保存文件运行:
 
运行程序:
程序运行
当点击Edit编辑,进入编辑视图。
 

用户可以删除数据或者调整顺序。


点击Add进行添加一行。

 
 
 
 

引自:其他,作为分享

TableViewCell,TableView,UITableViewCell的更多相关文章

  1. ‎Cocos2d-x 学习笔记(22) TableView

    [Cocos2d-x 学习笔记 ]目录 1. 简介 TableView直接继承了ScrollView和ScrollViewDelegate.说明TableView能实现ScrollView拖动cont ...

  2. iOS 学习 - 4.存储聊天记录

    主要是用sqlite3来存储聊天记录 先导入sqlite3.dylib, 点 Add Other,同时按住shift+command+G, 在弹出的Go to the folder中输入/usr/li ...

  3. UITableView多选全选

    自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableViewCell : UITabl ...

  4. iOS开发——UI篇OC篇&UITableView多项选择

    UITableView多项选择 自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableV ...

  5. swift 分页视图

    var data:NSArray! var scrollView: UIScrollView! var pageCtrl: UIPageControl! override func viewDidLo ...

  6. 为UITableViewController瘦身

    在IOS开发中采用了MVC得模式,ViewController通常是最庞大的文件,里面包含了各种各样的大概,造成代码的复用率低下,可读性也降低,那么有什么办法来解决这个问题呢. 在创建Table的时候 ...

  7. IOS开发-表视图LV3导航控制器

    学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...

  8. 简化MonoTouch.Dialog的使用

    读了一位园友写的使用MonoTouch.Dialog简化iOS界面开发,我来做个补充: 相信使用过DialogViewController(以下简称DVC)的同学都知道它的强大,但是缺点也是明显的,应 ...

  9. 项目管理之 Objective-C 编码规范

    目录: 一.格式化代码 二.命名 命名要求 1. 类的命名: 规则: 大驼峰命名法,每个单词的首字母都采用大写字母.一般添加业务前缀.后缀一般是当前类的种类. ViewController:后缀:Vi ...

随机推荐

  1. Ogre中OIS的输入的使用

    OIS的使用有两种模式:缓冲模式和非缓冲模式.非缓冲方式能处理鼠标或键盘长按的事件,实时性强缓冲方式则不能长按事件,用于非实时响应. 非缓冲输入:1.创建方式:其中 false 参数代表使用非缓冲模式 ...

  2. linux下vi命令修改文件及保存的使用方法

    进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi n filename :打开文件,并将光标置于第n行首 vi filename :打开文件,并将光标置于一行首 v ...

  3. iis 7.5 0x80004005 静态文件 html、js、css 500错误

    环境:iis 7.5 win7 64位 vs2012 问题:本地环境F5直接运行,没有任何问题,发布到IIS,静态文件不能访问,出现500错误,网上找了一堆解决办法,排除路径不正确,iis全部功能勾了 ...

  4. asp.net环境搭建

    win7 开启internet infornation server 勾选相应配置 管理设置里,新增网站,对网站进行配置,设置用户验证连接 根目录下,配置文件web.config

  5. MyBatis原理分析之四:一次SQL查询的源码分析

    上回我们讲到Mybatis加载相关的配置文件进行初始化,这回我们讲一下一次SQL查询怎么进行的. 准备工作 Mybatis完成一次SQL查询需要使用的代码如下: ) { ); ) { throw ne ...

  6. protobuf C++ 使用示例

    1.在.proto文件中定义消息格式 2.使用protobuf编译器 3.使用c++ api来读写消息 0.为何使用protobuf? 1.原始内存数据结构,可以以二进制方式sent/saved.这种 ...

  7. meta 标签 详细说明

    meta 标签可提供页面元素信息, 使用键值对的定义方式,可以记录网页上的主要信息,也可以自定义键值对 属性 content(必须),name,http-equiv,scheme,lang 常用 me ...

  8. 3.2 一般的哈尔空间Vj

    例3.2给予我们继续往下面做的动力.很明显的我们对于g(t)的逼近还是太粗糙了.很自然的,我们会想到,如果继续细分我们的短点,比如每1/2取一个值,甚至每1/4取一个值,那么就会有更好的逼近效果. 不 ...

  9. left join 取一条数据

    产品生产过程中要记录产品在制作过程中的不良历史,即使在一个工序,也可以产生多条不良历史记录,所有的不良信息保存在B表中.假设产品在打包工序,存放打包工序成品参数记录的表A,数据是唯一的.但在打包过程中 ...

  10. phpstrom 10 激活

    随着 JetBrains 新版本的发布,注册机已然不行了.然而,道高一尺,魔高一丈.IntelliJ IDEA开源社区 提供了如下通用激活方法:注册时选择License server填写http:// ...