UITableView基本属性二:编辑模式

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; //自定义
     */
}

系统自带的                                       自定义后的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值