UItableView编辑的使用edit, UItableView的一些常用方法
2014-07-20 22:01
344 查看
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.arr = [NSMutableArrayarrayWithObjects:@"li",@"yang",
@"xie",@"zhan",@"wang",@"huang",
@"qiang",@"yangyang",nil];
}
return
self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
self.tableView.separatorColor = [UIColorpurpleColor];
self.tableView.rowHeight =70;
self.tableView.dataSource =self;
self.tableView.delegate =self;
//
让tebleView处于编辑状态
[self.tableViewsetEditing:NOanimated:YES];
//设置一个按钮
开启/关闭编辑状态
self.navigationItem.leftBarButtonItem =self.editButtonItem;
[self.viewaddSubview:self.tableView];
[_tableView
release];
}
// 设置编辑的样式(删除/添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (0 == indexPath.row) {
returnUITableViewCellEditingStyleInsert;
}
//设置编辑的样式 (增加或删除)
returnUITableViewCellEditingStyleDelete;
}
// 编辑状态的点击方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[supersetEditing:editing
animated:animated];
[_tableViewsetEditing:editing
animated:animated];
}
// 点击edit/done时,实行的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//
当这个indexPath对应的cell
被标记为 删除
状态时
if (UITableViewCellEditingStyleDelete == editingStyle) {
//
删除一个cell
//1.
删除数组中对应的数据
[self.arrremoveObjectAtIndex:indexPath.row];
//
删除cell
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
}
if (UITableViewCellEditingStyleInsert == editingStyle) {
[self.arrinsertObject:@"11"atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationRight];
}
}
// 判断某一行
能不能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath
{
//
第三行不能编辑
if (3 == indexPath.row) {
return
NO;
}
return
YES;
}
// 移动
// 设定某一行能否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath
*)indexPath
{
return
YES;
}
// 从哪移动到哪
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//获取到原位置的
这条数据
NSString *name = [self.arrobjectAtIndex:sourceIndexPath.row];
//从原来的位置
删除掉这条数据
[self.arrremoveObjectAtIndex:sourceIndexPath.row];
//
添加到目的位置
[self.arrinsertObject:name
atIndex:destinationIndexPath.row];
}
// 有多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrcount];
}
// cell里是什么样的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *name =@"chong";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:name];
if (nil == cell) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:name];
}
cell.textLabel.text = [self.arrobjectAtIndex:indexPath.row];
cell.textLabel.text = [self.arrobjectAtIndex:indexPath.row];
return cell;
}
// tableView的一些常用方法
// 设置多个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 20;
}
// 设置分区的标题头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString
stringWithFormat:@"分区:%d", section];
}
// 设置section标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
// 利用协议方法控制cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (0 == indexPath.row) {
return 200;
}
return 100;
}
// 自定义view的section头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel
alloc ] initWithFrame:CGRectMake(0,
0, 0,
0)];
label.text = [NSString
stringWithFormat:@"分区:%d", section];
label.backgroundColor = [UIColor
purpleColor];
label.textColor = [UIColor
blackColor];
label.textAlignment =
NSTextAlignmentCenter;
return [label autorelease];
}
{
self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.arr = [NSMutableArrayarrayWithObjects:@"li",@"yang",
@"xie",@"zhan",@"wang",@"huang",
@"qiang",@"yangyang",nil];
}
return
self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
self.tableView.separatorColor = [UIColorpurpleColor];
self.tableView.rowHeight =70;
self.tableView.dataSource =self;
self.tableView.delegate =self;
//
让tebleView处于编辑状态
[self.tableViewsetEditing:NOanimated:YES];
//设置一个按钮
开启/关闭编辑状态
self.navigationItem.leftBarButtonItem =self.editButtonItem;
[self.viewaddSubview:self.tableView];
[_tableView
release];
}
// 设置编辑的样式(删除/添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (0 == indexPath.row) {
returnUITableViewCellEditingStyleInsert;
}
//设置编辑的样式 (增加或删除)
returnUITableViewCellEditingStyleDelete;
}
// 编辑状态的点击方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[supersetEditing:editing
animated:animated];
[_tableViewsetEditing:editing
animated:animated];
}
// 点击edit/done时,实行的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//
当这个indexPath对应的cell
被标记为 删除
状态时
if (UITableViewCellEditingStyleDelete == editingStyle) {
//
删除一个cell
//1.
删除数组中对应的数据
[self.arrremoveObjectAtIndex:indexPath.row];
//
删除cell
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
}
if (UITableViewCellEditingStyleInsert == editingStyle) {
[self.arrinsertObject:@"11"atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationRight];
}
}
// 判断某一行
能不能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath
{
//
第三行不能编辑
if (3 == indexPath.row) {
return
NO;
}
return
YES;
}
// 移动
// 设定某一行能否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath
*)indexPath
{
return
YES;
}
// 从哪移动到哪
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//获取到原位置的
这条数据
NSString *name = [self.arrobjectAtIndex:sourceIndexPath.row];
//从原来的位置
删除掉这条数据
[self.arrremoveObjectAtIndex:sourceIndexPath.row];
//
添加到目的位置
[self.arrinsertObject:name
atIndex:destinationIndexPath.row];
}
// 有多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrcount];
}
// cell里是什么样的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *name =@"chong";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:name];
if (nil == cell) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:name];
}
cell.textLabel.text = [self.arrobjectAtIndex:indexPath.row];
cell.textLabel.text = [self.arrobjectAtIndex:indexPath.row];
return cell;
}
// tableView的一些常用方法
// 设置多个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 20;
}
// 设置分区的标题头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString
stringWithFormat:@"分区:%d", section];
}
// 设置section标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
// 利用协议方法控制cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (0 == indexPath.row) {
return 200;
}
return 100;
}
// 自定义view的section头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel
alloc ] initWithFrame:CGRectMake(0,
0, 0,
0)];
label.text = [NSString
stringWithFormat:@"分区:%d", section];
label.backgroundColor = [UIColor
purpleColor];
label.textColor = [UIColor
blackColor];
label.textAlignment =
NSTextAlignmentCenter;
return [label autorelease];
}
相关文章推荐
- UITableView的一些常用方法
- IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
- 常用控件的使用方法--(TextView/EditText/Button/Menu) -计算器代码
- UITableView 使用及常用方法介绍
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- Android 第五课 常用控件的使用方法(TextView、Button、EditView、 ImageView、 ProgressBar、 ProgressDialog等)
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
- UITableView的一些常用设置和代理方法
- UIScrollView的基本使用和一些常用代理方法
- iOS UITableView的一些常用属性,方法
- TableViewController 使用,TableView 一些常用的方法
- IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
- uitableview 的一些使用方法
- 一些常用的工具使用方法
- UITableView 基本使用方法总结
- UITableView 基本使用方法
- 常见控件的使用方法 TextView EditText Button Menu
- UITableView 基本使用方法总结
- UITableView 基本使用方法
- UITableView 基本使用方法