这次的学习是在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进行添加一行。

引自:其他,作为分享

转载于:https://www.cnblogs.com/Wild-orangutans/p/3835024.html

TableViewCell,TableView,UITableViewCell相关推荐

  1. iOS 点击tableView的cell,让其滚到屏幕顶部

    点击tableView的cell,让其滚到屏幕顶部,很多电商的分类模块,都采用这种做法 1. 示例代码 - (void)viewDidLoad { [super viewDidLoad]; [self ...

  2. UITableView的优化原理

    2019独角兽企业重金招聘Python工程师标准>>> 当我们下啦一个 UITableView时,如果没有做优化,只是简单的实现功能代码如下,这样当我们有上百条tableviewce ...

  3. 解析 数据库 苹果自带地图

    首先把MJExtension 第三方文件拖入到工程里面 Model类和View类里面照常写东西 View里面用xib照常拖 然后在VC.m里面引入#import "AFNetworking/ ...

  4. iOS GameCenter 挑战,排名

    你也许曾听说过Game Center,它是自打iOS 4.1被引入的在线多人社交游戏网络,支持玩家邀请好友一起玩儿游戏,还可以建立一个多人游戏的会话,追踪成就系统,以及其他功能. 除了可以让开发者更轻 ...

  5. IOS仿支付宝首页滑动效果

    项目来源翻译大神的swift 本版为objectc版本, 大神地址: 这里写链接内容 一.效果图如下: 没什么逻辑可讲述的,直接给源码吧: // // ViewController.m // ZFBH ...

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

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

  7. UI一揽子计划 9 (UITableView 、UITableView 、重用机制)

    一. UITableView UITableView继承自UIScrollView,所以可以滚动 表视图的每⼀一条数据都是显示在UITableViewCell对象中 表视图可以分区显⽰示数据,每个分区 ...

  8. iOS 自定义cell

    思路 1.新建TAYTableViewCell 继承自UITableViewCell 2.添加需要使用的属性 3.写方法 4.应用 新建TAYTableViewCell 如图: 添加需要的属性 以一个 ...

  9. IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte

    1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...

最新文章

  1. C++标准库简介(转)
  2. 对于七段数码数字模型进行改进:一个关键的数字1的问题
  3. 思考:通过MMU/TLB/Cache对安全内存攻击的可能性
  4. C#隐藏任务栏区图标(非右下角托盘)
  5. -bash: /tyrone/jdk/jdk1.8.0_91/bin/java: cannot execute binary file
  6. 编程到底难在哪里? 从一个美国实习生的故事说起
  7. 笔记45 | 代码性能优化建议[转]
  8. 信号处理:单边、双边频谱间的相互转换(转)
  9. gcc/g++超详细上手教程
  10. Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.2:testCompile问题解决
  11. 主干网络系列(2) -ResNet V2:深度残差网络中的恒等映射
  12. idea导入gradle项目ctrl无法定位问题
  13. Firefox 扩展“此组件无法安装,因为它未通过验证。”问题的解决
  14. 华师计算机学院在职研究生,2021年华南师范大学在职研究生招生简章
  15. Python提取图片中的文字信息
  16. 《部落冲突:皇室战争》——一款不能错过的游戏!
  17. 【保卫萝卜】笔记 1
  18. 电脑C盘爆满了怎么办
  19. 基于Maven+SpringMVC+Spring+MyBatis+Layui整合框架,超详细的SSM整合❤️
  20. [题解]LuoGu2698: [USACO12MAR]花盆Flowerpot

热门文章

  1. 读书笔记_打开量化投资的黑箱04
  2. php 改变图片的宽高,php缩放图片(根据宽高的等比例缩放)示例
  3. 深度好文,如何培养真正的数据分析思维?附实践案例
  4. java中的算数运算符号用法与原理分析
  5. 简单分析帆软报表中一次HTTP请求的过程。
  6. Flex控制对主机网页中脚本的访问
  7. 全局替换安卓应用字体
  8. php函数方法属性吗,为什么PHP属性不允许使用函数?
  9. python列表解析,生成表达式(一分钟读懂)
  10. 哈工大理论力学第八版电子版_理论力学哈工大第八版1第一章思考题课后题