您的位置:首页 > 产品设计 > UI/UE

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: