TableViewCell,TableView,UITableViewCell

这次的学习是在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

时间: 2024-08-30 08:09:18

TableViewCell,TableView,UITableViewCell的相关文章

ios 自定义tableViewcell,UITableViewCell

//自定义tableViewcell,当系统自带的tableView样式无法满足我们的需求可通过代码实现自定义cell demo效果看附件 #import <UIKit/UIKit.h> #import "Houses.h" #define kRowWidth [UIScreen mainScreen].bounds.size.width #define kRowHeight 90 @interface HouseTableViewCell : UITableViewCel

Swift基础--tableview练习

新手练习Swift版tableview //  ViewController.swift //  Swift_lianxi //  Created by dllo on 16/1/7. //  Copyright © 2016年 z_han. All rights reserved. // import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var ta

Snail—UI学习之表视图TableView(一)

我们是整一个表视图 然后再表视图的上方是一个广告栏 首先,在.h文件中写上下面的代码 主要就是遵守一些代理 #import <UIKit/UIKit.h> @interface WJJRootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate> @end 然后再.m文件中写上如下 #import "WJJRootViewContro

Snail—UI学习之表视图TableView(二)

接着上面的项目 ,当下面标记红色的代码写上后,我们按下右上角的edit按钮 就可以对cell进行插入.删除.移动等操作 #import "WJJRootViewController.h" @interface WJJRootViewController (){ //数据源 存放数据 NSMutableArray * _dataArray; //这就是我们的tableView UITableView * _tableView; //页面控制器 UIPageControl * _pageC

Snail—UI学习之表视图TableView多行删除

这次实现的功能是多行cell进行删除 代码是在上一次的基础上进行修改的 有的代码删除重写 有的方法只是加了一些逻辑判断 // // WJJRootViewController.m // blog_UITableView // // Created by Snail on 15-7-30. // Copyright (c) 2015年 Snail. All rights reserved. // #import "WJJRootViewController.h" @interface W

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

点击tableView的cell,让其滚到屏幕顶部,很多电商的分类模块,都采用这种做法 1. 示例代码 - (void)viewDidLoad { [super viewDidLoad]; [self addTableView]; } #pragma mark - 创建tableView - (void)addTableView { UITableView *tableView = [[UITableView alloc]init]; tableView.frame = self.view.bo

iOS学习4.存储聊天记录

主要是用sqlite3来存储聊天记录 先导入sqlite3.dylib, 点 Add Other,同时按住shift+command+G, 在弹出的Go to the folder中输入/usr/lib/libsqlite3.dylib,就OK了. 还需要import<sqlite3.h> 1.new file一个Text类用来存储,.m无需操作 1 #import <Foundation/Foundation.h> 2 3 @interface Text : NSObject 4

UITableView多选全选

自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableViewCell : UITableViewCell { BOOL _checked; UIImageView *_checkedImage; } - (void)setChecked:(BOOL)checked; @end TableViewCell.m #import "TableViewCell.h" @implem

iOS开发——UI篇OC篇&amp;UITableView多项选择

UITableView多项选择 自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableViewCell : UITableViewCell { BOOL _checked; UIImageView *_checkedImage; } - (void)setChecked:(BOOL)checked; @end TableViewCell.m #import "TableViewCell