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

UITableView基本属性二:编辑模式

2015-09-06 14:29 423 查看
UITableView基本属性二 :

//设置分区头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (0 == section) {
return @"男员工";
}else{
return @"女员工";
}
}

//设置左滑文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"解绑";
}

//编辑模式  左边“+” “-”
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//删除男员工
if (editingStyle == UITableViewCellEditingStyleDelete) {
[[self.allPerson objectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row]; //删除数据
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; //删除行
}
else{
NSLog(@"添加女员工");
TRpeson *newperson = [[TRpeson alloc]init];
newperson.no = 140801;
newperson.name = @"新员工";

[[self.allPerson objectAtIndex:indexPath.section]insertObject:newperson atIndex:indexPath.row]; //添加数据
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; //添加行
}
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (0 == indexPath.section) {
return UITableViewCellEditingStyleDelete; //删除
}
else{
return UITableViewCellEditingStyleInsert; //添加
}
}




//设置左边的“+”“-”
- (void)viewDidLoad
{
self.editButtonItem.possibleTitles = [NSSet setWithObjects:@"编辑", @"完成", nil];
self.editButtonItem.title = @"编辑";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

//编辑模式
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated]; //

if (self.editing) {
self.editButtonItem.title = @"完成";
} else {
self.editButtonItem.title = @"编辑";
}
}


#pragma mark 移动行
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

//拿到要移动的对象
TRpeson *person=[[self.allPerson objectAtIndex:sourceIndexPath.section]objectAtIndex:sourceIndexPath.row];
//删除原来的位置
[[self.allPerson objectAtIndex:sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];
//更新数据
[[self.allPerson objectAtIndex:destinationIndexPath.section]insertObject:person atIndex:destinationIndexPath.row];
[self.tableView reloadData];
}



//Cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Cell被点击了");
UITableViewCell *ontcell=[tableView cellForRowAtIndexPath:indexPath];//拿到点击cell对象
if (ontcell.accessoryType==UITableViewCellAccessoryNone) {
ontcell.accessoryType=UITableViewCellAccessoryCheckmark;

}else{
ontcell.accessoryType=UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //动画渐变

}
/*
cell.accessoryType = UITableViewCellAccessoryNone;//cell没有任何的样式

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;

cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;

UIImage *image= [UIImage   imageNamed:@"delete.png"];

button = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);

button.frame = frame;

[button setBackgroundImage:imageforState:UIControlStateNormal];

button.backgroundColor= [UIColor clearColor];

cell.accessoryView= button; //自定义
*/
}

系统自带的

自定义后的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: