tableViewcell 的右移多选删除

跟据需求,点击右上角的编辑按钮,tabview编辑模式选中左边按钮变蓝,(这个实在自定义的cell中改变了系统的cell),优选中的时候删除按钮可点击文字变成蓝色并显示上出的条数,全选按钮的点击全选变成蓝色,取消全选变成灰。具体的键代码

1.编辑前:

这里写图片描述

2.编辑的时候

这里写图片描述

代码

1 #import “xxxxxxxx.h”代码

#import "NewFriendViewController.h"
#import "NewFriendModel.h"
#import "AddFriendViewController.h"
#import "NewFriendCell.h"
#import "PepRegViewController.h"
#import "NIMSDK.h"
#import "NIMMessageMaker.h"
#import "NIMSession.h"

@interface NewFriendViewController ()<NewFriendDelegate>

/** 底部的View  */
@property (strong, nonatomic) UIView * BottomView;
/** 删除的按钮  */
@property (strong, nonatomic) UIButton *deleBtn;
/** 全选按钮  */
@property (strong, nonatomic) UIButton *AllBtn;
/** 全选按钮图片  */
@property (strong, nonatomic) UIImageView * AllimagView;
/** 全选按钮文字  */
@property (strong, nonatomic) UILabel *AllLable;
/** 删除好友的数组 */
@property (strong, nonatomic) NSMutableArray * deleteArray;
/** 编辑的样式  */
@property (assign, nonatomic) UITableViewCellEditingStyle EditingStyle;


@end

@implementation NewFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [SFAnalyticsManager eventNewFriendList];
     self.navigationItem.title = @"新朋友";
    [self.tableView registerClass:[NewFriendCell class] forCellReuseIdentifier:@"NewFriendCell"];
     BACKCOLOR;
     self.dataArray = [[NSMutableArray alloc]init];
    self.tableView.sectionFooterHeight = 0.0f;
    self.tableView.sectionHeaderHeight = 0.0f;
     self.view.backgroundColor = SFColorHexString(@"efefef");
    self.tableView.editing = NO;
    self.tableView.frame = CGRectMake(0, 64, SFScreenW, SFScreenH - 64);
    self.EditingStyle = UITableViewCellEditingStyleDelete;
    [self setExtraCellLineHidden:self.tableView];
     [self createUI];
     [self downRefresh];
}
-(void)createUI {
    typeof(self) __weak weakSelf = self;
    [SFPromptError shareInstance].block = ^{
        [weakSelf downRefresh];
    };
      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编 辑" style:UIBarButtonItemStylePlain target:self action:@selector(makeFriend)];

    [self.view addSubview:self.BottomView];
    [self.BottomView addSubview:self.deleBtn];
    [self.deleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.BottomView.mas_centerY).offset(0);
        make.right.equalTo(@0);
        make.width.equalTo(@160);
        make.height.equalTo(@45);
    }];

    [self.BottomView addSubview:self.AllBtn];
    [self.AllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.BottomView.mas_centerY).offset(0);
        make.left.equalTo(@0);
        make.width.equalTo(@(SFScreenW - 160));
        make.height.equalTo(@45);
    }];

    [self.BottomView addSubview:self.AllimagView];
    [self.AllimagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.BottomView.mas_centerY).offset(0);
        make.left.equalTo(@15);
        make.width.equalTo(@15);
        make.height.equalTo(@15);
    }];

    [self.BottomView addSubview:self.AllLable];
    [self.AllLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.BottomView.mas_centerY).offset(0);
        make.left.equalTo(self.AllimagView.mas_right).offset(12);
    }];

}

- (void)downRefresh{

    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
//    [header beginRefreshing];
    self.tableView.mj_header = header;
    [self createData];
}

#pragma mark--初始化数据源--
-(void)createData {
     NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
     params[@"accid"] = USERDEFULT_READ(@"accid");
     params[@"access_token"] = USERDEFULT_READ(@"access_token");
    if (![self.tableView.mj_header isRefreshing]) {
        [SFMaskView showWithMaskViewAddTo:self.view andDisabledButton:nil];
    }

     [SFHttpTool get:GET_FRD params:params success:^(id json) {
         [SFMaskView removeWithMaskViewFrom:self.view andDisabledButton:nil];
          if ([json[@"code"]intValue] == 200) {
              [self.dataArray removeAllObjects];
              NSLog(@"获取好友信息:%@",json);
              if ([json[@"data"] count] == 0) {
                [[SFPromptError shareInstance]showPromptAddedTo:self.tableView andOffsetY:0 andMessage:@"抱歉!暂无数据"];
              }else{
                  for (NSDictionary *dic in json[@"data"]) {

                      NewFriendModel *friend = [[NewFriendModel alloc]init];
                      [friend setValuesForKeysWithDictionary:dic];
                      friend.isSelect = NO;
                      [self.dataArray addObject:friend];
                  }
                    [[SFPromptError shareInstance]hide];
              }

              if (self.dataArray.count == 0) {
                  self.navigationItem.rightBarButtonItem = nil ;
              }

              [self.tableView reloadData];
              [self.tableView.mj_header endRefreshing];
          }else{
              [self.tableView.mj_header endRefreshing];
              [[SFPromptError shareInstance]showPromptAddedTo:self.tableView andOffsetY:0 andMessage:json[@"msg"]];
          }

     } failure:^(NSError *error) {
         [SFMaskView removeWithMaskViewFrom:self.view andDisabledButton:nil];
         [self.tableView.mj_header endRefreshing];
          [[SFPromptError shareInstance]showPromptAddedTo:self.tableView andOffsetY:0 andMessage:@"哎呀!网络出了点小问题"];
     }];
}
#pragma mark--cell的代理--
- (void)onAccept:(NewFriendCell *)cell  {

    NewFriendModel *friendModel = cell.model;
     NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
     params[@"accid"] = USERDEFULT_READ(@"accid");
     params[@"faccid"] = friendModel.accId;
     params[@"access_token"] = USERDEFULT_READ(@"access_token");
     [SFMaskView showWithMaskViewAddTo:nil andDisabledButton:nil];
     [SFHttpTool put:GET_FRD params:params success:^(id json) {
          if ([json[@"code"]intValue] == 200) {
               [[SFPromptError shareInstance]hide];
               [SFMaskView removeWithMaskViewFrom:nil andDisabledButton:nil];
               UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"已接受好友申请"  preferredStyle:UIAlertControllerStyleAlert];
               UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //self.newFridBlock(YES);
                    USERDEFULT_ADD(@"1", @"add");
//                    [self createData];
                   [self setAcceptButton:cell.accpetBtn];
                   //发送消息

                   NIMSession *session = [NIMSession session:friendModel.accId type:NIMSessionTypeP2P];
                   [[NIMSDK sharedSDK].chatManager sendMessage:[NIMMessageMaker msgWithText:@"我们已经是好友了,现在可以开始聊天了~"] toSession:session error:nil];
               }];

               [alertC addAction:action];

               [self presentViewController:alertC animated:YES completion:nil];

               }else{
                   [SFMaskView removeWithMaskViewFrom:nil andDisabledButton:nil];
                   [[SFPromptError shareInstance]showPromptAddedTo:self.view andOffsetY:0 andMessage:@"哎呀!网络出了点小问题"];
          }


     } failure:^(NSError *error) {
          [SFMaskView removeWithMaskViewFrom:nil andDisabledButton:nil];
            [[SFPromptError shareInstance]showPromptAddedTo:self.view andOffsetY:0 andMessage:@"哎呀!网络出了点小问题"];
     }];

}
- (void)setAcceptButton:(UIButton *)button {

    [button setTitle:@"已添加" forState:UIControlStateNormal];
    [button setTitleColor:SFColorHexString(@"888888")  forState:UIControlStateNormal];
    button.layer.borderColor = SFColorHexString(@"888888").CGColor;
    button.titleLabel.font = SFSystemFont(13.0);
    button.enabled = NO;
    button.backgroundColor = [UIColor whiteColor];
    button.tintColor = [UIColor grayColor];
}
#pragma  mark ---添加好友界面---
#pragma  mark ---编辑按钮的点击 ---
-(void)makeFriend {

    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编 辑"]) {

        [self.tableView setEditing:YES animated:YES];
        self.BottomView.hidden = NO;
        self.navigationItem.rightBarButtonItem.title = @"完 成";
        self.AllimagView.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        [self.AllBtn setTitleColor:SFColorHexString(SFButtonDisable2Color) forState:UIControlStateNormal];
        self.AllBtn.selected = NO;
         [self.deleBtn setTitle:@"删除" forState:UIControlStateNormal];
        self.deleBtn.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        [self.deleBtn setTitleColor:SFColorHexString(SFBlackColor) forState:UIControlStateNormal];
        [self.deleteArray removeAllObjects];
        self.deleBtn.enabled = NO;
        self.tableView.mj_header.hidden = YES;


    }else{

        [self.tableView setEditing:NO animated:YES];
        self.BottomView.hidden = YES;
        self.navigationItem.rightBarButtonItem.title = @"编 辑";

        self.AllBtn.selected = NO;
        [self.deleBtn setTitle:@"删除" forState:UIControlStateNormal];
        self.deleBtn.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        [self.deleBtn setTitleColor:SFColorHexString(SFBlackColor) forState:UIControlStateNormal];
        [self.deleteArray removeAllObjects];
        self.deleBtn.enabled = NO;
        self.tableView.mj_header.hidden = NO;

    }

}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

#pragma mark ---<UITableViewDelegate,UITableViewDataSource>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NewFriendCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"NewFriendCell" forIndexPath:indexPath];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.NewFriendDelegate = self;
     cell.model = self.dataArray[indexPath.row];
//     cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(whenClickImage:)];
    cell.imageV.tag = indexPath.row;
    cell.imageV.userInteractionEnabled = YES;
    [cell.imageV addGestureRecognizer:singleTap];

     return cell;
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(NewFriendCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
    
    
//    NewFriendModel *friend = self.dataArray[indexPath.row];
//
//    [cell updateModel:friend];
//}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {

    return 78;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;
}

//选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.tableView.editing == YES) {

        NewFriendCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        for (UIView *v1 in cell.subviews) {
            NSLog(@"cell.subview---%@", v1);
            if ([v1 isKindOfClass:[UIControl class]]) {
                for (UIView *v2 in v1.subviews) {
                    NSLog(@"UITableViewCellEditControl.subview---%@", v2);
                    if ([v2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView = (UIImageView *)v2;
                        imageView.image = [UIImage imageNamed:@"yuanlan"];
                    }
                }
            }
        }

        NewFriendModel *friend = self.dataArray[indexPath.row];
        friend.isSelect = YES;
        [self.deleteArray addObject:friend];
        NSString * str = [NSString stringWithFormat:@"删除(%ld)",self.deleteArray.count];
        [self.deleBtn setTitle:str forState:UIControlStateNormal];
        if (self.deleteArray.count == self.dataArray.count) {
            self.AllimagView.backgroundColor = SFColorHexString(SFBlueColor);
            self.AllLable.textColor = SFColorHexString(SFBlueColor);
        }
        if (self.deleteArray.count !=0){

            self.deleBtn.enabled = YES;
            [self.deleBtn setTitleColor:SFColorHexString(SFWhiteColor) forState:UIControlStateNormal];
            self.deleBtn.backgroundColor = SFColorHexString(SFBlueColor);
        }

    }else{

    }


}


//取消选中时 将存放在self.deleteArr中的数据移除

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {

    NewFriendCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (UIView *v1 in cell.subviews) {
        NSLog(@"cell.subview---%@", v1);
        if ([v1 isKindOfClass:[UIControl class]]) {
            for (UIView *v2 in v1.subviews) {
                NSLog(@"UITableViewCellEditControl.subview---%@", v2);
                if ([v2 isKindOfClass:[UIImageView class]]) {
                    UIImageView *imageView = (UIImageView *)v2;
                    imageView.image = [UIImage imageNamed:@"yuanhui"];

                }
            }
        }
    }
    NewFriendModel *friend = self.dataArray[indexPath.row];
    friend.isSelect = NO;
    [self.deleteArray removeObject:friend];
    NSString * str ;
    if (self.deleteArray.count == 0) {
         str = @"删除";
    }else{
        str = [NSString stringWithFormat:@"删除(%ld)",self.deleteArray.count];
    }

    [self.deleBtn setTitle:str forState:UIControlStateNormal];
    [self.deleBtn setTitleColor:SFColorHexString(SFWhiteColor) forState:UIControlStateNormal];
    self.deleBtn.backgroundColor = SFColorHexString(SFBlueColor);
    if (self.deleteArray.count != self.dataArray.count) {
        self.AllimagView.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        self.AllLable.textColor =  SFColorHexString(SFButtonDisable2Color);
    }
    if (self.deleteArray.count ==0) {
        self.deleBtn.enabled = NO;
        [self.deleBtn setTitleColor:SFColorHexString(SFBlackColor) forState:UIControlStateNormal];
        self.deleBtn.backgroundColor = SFColorHexString(SFButtonDisable2Color);
    }
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        删除选中行
        NewFriendModel *friend = self.dataArray[indexPath.row];
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        params[@"accid"] = USERDEFULT_READ(@"accid");
        params[@"faccid"] = friend.accId;
        params[@"access_token"] = USERDEFULT_READ(@"access_token");
        [SFHttpTool del:GET_FRD params:params success:^(id json) {
            if ([json[@"code"]intValue] == 200) {
                SFLog(@"删除好友成功");
                [self.dataArray removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }else{
                SFLog(@"删除好友申请 code%@,msg%@",json[@"code"],json[@"msg"]);
            }
        } failure:^(NSError *error) {
            SFLog(@"删除好友申请 %@",error);
        }];

    }
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)whenClickImage:(UITapGestureRecognizer *)recognizer{
    NSLog(@"点击了头像");
    NewFriendModel *model = self.dataArray[recognizer.view.tag];
    PepRegViewController *detailVC = [[PepRegViewController alloc] init];
    detailVC.accId = model.accId;
    [self.navigationController pushViewController:detailVC animated:YES];

}

- (void)setExtraCellLineHidden: (UITableView *)tableView{

    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor redColor];
    [tableView setTableFooterView:view];
}

#pragma mark --------全选按钮的实现 ----------
- (void)AllBtnClick:(UIButton *)AllBtn{

    if (self.dataArray.count == 0) {
        return;
    }
    [self.deleteArray removeAllObjects];
    if (AllBtn.selected == YES) {


        AllBtn.selected = NO;
        SFLog(@"取消全选");
        self.AllimagView.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        self.AllLable.textColor = SFColorHexString(SFButtonDisable2Color);
        for (int i = 0; i < self.dataArray.count; i ++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
//            [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            [self tableView:self.tableView didDeselectRowAtIndexPath:indexPath];
            [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

        }
        SFLog(@"%ld",self.deleteArray.count);
          NSString * str = [NSString stringWithFormat:@"删除"];
        [self.deleBtn setTitle:str forState:UIControlStateNormal];
        [self.deleBtn setTitleColor:SFColorHexString(SFBlackColor) forState:UIControlStateNormal];
        self.deleBtn.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        self.deleBtn.enabled = NO;


    }else{

        AllBtn.selected = YES;
        SFLog(@"全选");
        self.AllimagView.backgroundColor = SFColorHexString(SFBlueColor);
        self.AllLable.textColor = SFColorHexString(SFBlueColor);

        for (int i = 0; i < self.dataArray.count; i ++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
            [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
        }
//        [self.deleteArray addObjectsFromArray:self.dataArray];

        NSString * str = [NSString stringWithFormat:@"删除(%ld)",self.dataArray.count];
        [self.deleBtn setTitle:str forState:UIControlStateNormal];
        [self.deleBtn setTitleColor:SFColorHexString(SFWhiteColor) forState:UIControlStateNormal];
        self.deleBtn.backgroundColor = SFColorHexString(SFBlueColor);
        self.deleBtn.enabled = YES;
    }

}
#pragma mark --------删除按钮的实现 ----------
- (void)deleBtnClick{

    UIAlertController * sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"删除所选新朋友记录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        SFLog(@"删除所有新朋友记录");

        NSMutableArray * faccidArr = [NSMutableArray array];
        NSString * faccidStr ;
        for (int i = 0; i < self.deleteArray.count; i ++) {
            NewFriendModel *friend = self.deleteArray[i];
            [faccidArr addObject:friend.accId];
        }
        faccidStr = [faccidArr componentsJoinedByString:@","];

        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        params[@"accid"] = USERDEFULT_READ(@"accid");
        params[@"faccid"] = faccidStr;
        params[@"access_token"] = USERDEFULT_READ(@"access_token");
        [SFHttpTool del:GET_FRD params:params success:^(id json) {
            if ([json[@"code"]intValue] == 200) {
                SFLog(@"删除好友成功");
                for (int i = 0; i < self.deleteArray.count; i ++) {
                    NewFriendModel *friend = self.deleteArray[i];
                    [self.dataArray removeObject:friend];
                }
                [self makeFriend];

                self.AllimagView.backgroundColor = SFColorHexString(SFButtonDisable2Color);
                self.AllLable.textColor = SFColorHexString(SFButtonDisable2Color);
                NSString * str = [NSString stringWithFormat:@"删除"];
                [self.deleBtn setTitle:str forState:UIControlStateNormal];
                [self.deleBtn setTitleColor:SFColorHexString(SFBlackColor) forState:UIControlStateNormal];
                self.deleBtn.backgroundColor = SFColorHexString(SFButtonDisable2Color);
                self.deleBtn.enabled = NO;

                [self.tableView reloadData];
            }else{
                SFLog(@"删除好友申请 code%@,msg%@",json[@"code"],json[@"msg"]);
            }
        } failure:^(NSError *error) {
            SFLog(@"删除好友申请 %@",error);
        }];


    }]];

    [self presentViewController:sheet animated:YES completion:nil];

}
#pragma mark -------- 点击编辑后底部显示的全选的View ------

- (UIView *)BottomView{
    if (_BottomView == nil) {
        _BottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SFScreenH - 45, SFScreenW, 45)];
        _BottomView.backgroundColor = SFColorHexString(SFWhiteColor);
        _BottomView.hidden = YES;
    }
    return _BottomView;

}
/** 全选按钮  */
- (UIButton *)AllBtn{
    if (_AllBtn == nil) {
        _AllBtn = [[UIButton alloc]init];

        [_AllBtn addTarget:self action:@selector(AllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _AllBtn;
}
/** 全选按钮图片  */
- (UIImageView *)AllimagView{
    if (_AllimagView == nil) {
        _AllimagView = [[UIImageView alloc]init];
//        _AllimagView.image = [UIImage imageNamed:@""];
        _AllimagView.backgroundColor =SFColorHexString(SFButtonDisable2Color);
        _AllimagView.layer.cornerRadius = 15/2 ;
        _AllimagView.layer.masksToBounds = YES;
    }
    return _AllimagView;
}
/** 全选按钮文字  */
- (UILabel *)AllLable{
    if (_AllLable == nil) {
        _AllLable = [[UILabel alloc]init];
        _AllLable.text = @"全选";
        _AllLable.textColor = SFColorHexString(SFButtonDisable2Color);
        _AllLable.font = [UIFont systemFontOfSize:13];
    }
    return _AllLable;
}
/** 删除的按钮  */
- (UIButton *)deleBtn{
    if (_deleBtn == nil) {
        _deleBtn = [[UIButton alloc]init];
        [_deleBtn setTitle:@"删除" forState:UIControlStateNormal];
        _deleBtn.backgroundColor = SFColorHexString(SFButtonDisable2Color);
        [_deleBtn setTitleColor:SFColorHexString(SFBlackColor) forState:UIControlStateNormal];
        _deleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
         [_deleBtn addTarget:self action:@selector(deleBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _deleBtn.enabled = NO;
    }
    return _deleBtn;
}
- (NSMutableArray *)deleteArray{
    if (_deleteArray == nil) {
        _deleteArray = [NSMutableArray array];
    }
    return _deleteArray;
}


@end
  1. cell代码

#import <UIKit/UIKit.h>
#import "NewFriendModel.h"

@class NewFriendModel;
@protocol NewFriendDelegate <NSObject>

- (void)onAccept:(UITableViewCell *)cell ;

@end


@interface NewFriendCell : UITableViewCell

@property (nonatomic,strong) UIImageView *imageV;

@property (nonatomic,strong) UILabel *nameLab;

@property (nonatomic,strong) UILabel *desLabel;

@property (nonatomic,strong) UIButton *accpetBtn;

@property (nonatomic,assign) id<NewFriendDelegate>NewFriendDelegate;

@property (nonatomic,strong) NewFriendModel *model;

/** <#type#>*/
@property (nonatomic,strong)UILabel *acceptLabel;
#import "NewFriendCell.h"

@interface NewFriendCell ()

@property (nonatomic,strong) NewFriendModel *friendModel;


@end

@implementation NewFriendCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

          [self creatUI];
     }

     return self;
}

-(void)creatUI {
    typeof(self) weSelf = self;

     self.imageV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15 , 48, 48)];
     [self.contentView addSubview:self.imageV];

     self.accpetBtn = [UIButton buttonWithType:UIButtonTypeSystem];
     [self.accpetBtn setTitle:@"接 受" forState:UIControlStateNormal];
     [self.accpetBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
     [self.accpetBtn addTarget:self action:@selector(onAccept) forControlEvents:UIControlEventTouchUpInside];
     self.accpetBtn.backgroundColor = [UIColor clearColor];
     self.accpetBtn.layer.cornerRadius = 5;
     self.accpetBtn.layer.masksToBounds = YES;
    self.accpetBtn.layer.borderWidth = 0.5;
     self.accpetBtn.layer.borderColor = SFColorHexString(SFBlueColor).CGColor;
//     self.accpetBtn.tintColor = SFColorHexString(SFBlueColor);
    [self.accpetBtn setTitleColor:SFColorHexString(SFBlueColor) forState:UIControlStateNormal];
     [self.contentView addSubview:self.accpetBtn];
     [self.accpetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.equalTo(weSelf.contentView.mas_centerY).equalTo(0);
//          make.top.equalTo(weSelf.contentView).with.offset(20);
          make.right.equalTo(weSelf.contentView).with.offset(-15);
//          make.bottom.equalTo(weSelf.contentView).with.offset(-20);
          make.width.equalTo(@60);
         make.height.equalTo(@25);
     }];

    self.nameLab = [[UILabel alloc]init];
    self.nameLab.font = [UIFont systemFontOfSize:17];
    [self.contentView addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weSelf.contentView).offset(20);
        make.left.mas_equalTo(weSelf.imageV.mas_right).offset(12);
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(weSelf.accpetBtn.mas_left).offset(-10);
    }];

    self.desLabel = [[UILabel alloc]init];
    self.desLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.desLabel];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(48);
        make.left.mas_equalTo(weSelf.nameLab);
        make.right.mas_equalTo(weSelf.nameLab);
        make.height.mas_equalTo(15);
    }];

    [self.contentView addSubview:self.acceptLabel];
    [self.acceptLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(-SFLeftMargin);
        make.centerY.mas_equalTo(self.contentView);
    }];


}

-(void)onAccept{
     if (_NewFriendDelegate && [_NewFriendDelegate respondsToSelector:@selector(onAccept:)]){
          [_NewFriendDelegate onAccept:self ];
     }
}

- (void)setModel:(NewFriendModel *)model{

    _model = model;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"默认头像"]];
    self.nameLab.text = model.nick;
    self.desLabel.text = model.inviteMsg;
    if ([model.state intValue]== 30) {

        [self.accpetBtn setTitle:@"已添加" forState:UIControlStateNormal];
        [self.accpetBtn setTitleColor:SFColorHexString(@"888888")  forState:UIControlStateNormal];
        self.accpetBtn.enabled = NO;
        self.accpetBtn.layer.borderColor = SFColorHexString(@"888888").CGColor;

    }else{

        [self.accpetBtn setTitle:@"接 受" forState:UIControlStateNormal];
        [self.accpetBtn setTitleColor:SFColorHexString(SFBlueColor) forState:UIControlStateNormal];
        self.accpetBtn.layer.borderColor = SFColorHexString(SFBlueColor).CGColor;
    }

}

- (void)updateModel:(NewFriendModel *)model
{
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *v in control.subviews)
            {
                if ([v isKindOfClass: [UIImageView class]]) {
                    UIImageView *img=(UIImageView *)v;
                    SFLog(@"---------- 2222");
                    img.image = [UIImage imageNamed:(model.isSelect ? @"yuanlan" : @"yuanhui")];

                }
            }
        }
    }
}



-(void)configWithModel:(NewFriendModel *)model {
     [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"默认头像"]];
     self.nameLab.text = model.nick;
    self.desLabel.text = model.inviteMsg;
     if ([model.state intValue]== 30) {
          [self.accpetBtn setTitle:@"已添加" forState:UIControlStateNormal];
         [self.accpetBtn setTitleColor:SFColorHexString(@"888888")  forState:UIControlStateNormal];
          self.accpetBtn.enabled = NO;
          self.accpetBtn.layer.borderColor = SFColorHexString(@"888888").CGColor;

     }
     self.model = model;

}


**- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *v in control.subviews)
            {
                if ([v isKindOfClass: [UIImageView class]]) {
                    UIImageView *img=(UIImageView *)v;

                    if (!self.selected) {
                        img.image=[UIImage imageNamed:@"yuanhui"];

                    }else{

                        img.image=[UIImage imageNamed:@"yuanlan"];

                    }
                }
            }
        }
    }

}**

- (UILabel *)acceptLabel {

    if (!_acceptLabel) {

        _acceptLabel = [[UILabel alloc] init];
        _acceptLabel.textColor = [UIColor lightGrayColor];
        _acceptLabel.text = @"已添加";
        _acceptLabel.hidden = YES;
        _acceptLabel.font = SFSystemFont(12.0);
        _acceptLabel.textColor = SFColorHexString(@"888888");
    }
    return _acceptLabel;
}

@end

猜你喜欢

转载自blog.csdn.net/woruosuifenglang/article/details/77867886