UItUItableView 自定义 多选和全选

来源:互联网 发布:淘宝网加绒运动服装 编辑:程序博客网 时间:2024/05/21 17:53

                         



      这个是StoryBoard写了

model 类

#import <Foundation/Foundation.h>


@interface My_CommodityModel : NSObject

//按钮选中

@property (nonatomic,assign) BOOL seleted;

//是否改变 移动

@property (nonatomic,assign) BOOL isMoved ;



@end



    My_MyCommodityTableViewCell

#

#import <UIKit/UIKit.h>

@interface My_MyCommodityTableViewCell : UITableViewCell

//最左边的选中图片

@property (weak, nonatomic) IBOutlet UIImageView *selectedImageView;

//商品介绍

@property (weak, nonatomic) IBOutlet UILabel *goodNamelabel;

@property (weak, nonatomic) IBOutlet UIView *moveBackgroundView;

//商品图片

@property (weak, nonatomic) IBOutlet UIImageView *goodsImageView;


@end



@end





//  这里主要用了 一个tableViewCell  两种标识符  防止Cell重用机制  


不会写博客,下面复制代码了 

#import "My_FootViewController.h"

#import "My_MyCommodityTableViewCell.h"

#import "My_CommodityModel.h"

@interface My_FootViewController ()

{

    BOOL isSeleted; //最上面 删除按钮 是否是选择状态

    NSInteger count; // 点击cell 选中状态的个数  用来判断全选按钮的状态  

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *deleteButton;//导航栏删除按钮


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewBottom;//tableView下面的约束 

@property (weak, nonatomic) IBOutlet UIView *bottomView;//最下面的View

//全选按钮

@property (weak, nonatomic) IBOutlet UIButton *allSeletedButton;//全选按钮


@property (nonatomic,strong)NSMutableArray *dataArray;


@end


@implementation My_FootViewController



-(void)awakeFromNib

{

    self.dataArray = [NSMutableArray array];

}



- (void)viewDidLoad {

    [super viewDidLoad];

    

    isSeleted = NO;

    self.bottomView.hidden = YES;

    for (int i = 0; i<10; i++) {

        My_CommodityModel *model = [[My_CommodityModel alloc]init];

        model.seleted = NO; //单选 多选的 标识符

        model.isMoved = NO;// 是否切换Cell 的标识符

        [self.dataArray addObject:model];

    }

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//导航栏删除按钮

- (IBAction)rightBtnEven:(UIButton *)sender {

    isSeleted = !isSeleted;

    count = 0 ;//这个是通过cellSeleted个数 来判断全选按钮Seleted 状态

    self.bottomView.hidden = !self.bottomView.hidden;

    self.allSeletedButton.selected = NO;

    

    if (isSeleted == NO) {

        [sender setTitle:@"删除" forState:UIControlStateNormal];

        self.tableViewBottom.constant = 0;

        [self ChangeAlldataForSeleted:NO isMoved:NO];

        

    }else{

        [sender setTitle:@"完成" forState:UIControlStateNormal];

        self.tableViewBottom.constant = 49;

        [self ChangeAlldataForSeleted:NO isMoved:YES];

    }


}

#pragma mark 导航栏


//返回按钮


- (IBAction)back:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}


//所有的modelseleted 变化  isMoved 变化

-(void)ChangeAlldataForSeleted:(BOOL)seleted isMoved:(BOOL)moved{

        for (My_CommodityModel *model in self.dataArray) {

            model.seleted = seleted;

            model.isMoved = moved;

        }

    [self.tableView reloadData];

}

#pragma mark buttomView

//删除按钮

- (IBAction)deleteButton:(id)sender {

    

        NSMutableArray *array= [NSMutableArray array];

        for (My_CommodityModel *model in self.dataArray) {

            if (model.seleted==YES) {

                [array addObject:model];

            }

        }

        [self.dataArray removeObjectsInArray:array];

        //访问删除的接口

    self.allSeletedButton.selected = NO;

    count = 0 ;

    if (self.dataArray.count == NO) {

        self.bottomView.hidden = YES;

        [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];

    }else{

        self.bottomView.hidden = NO;

    }

    [self.tableView reloadData];

}


//取消按钮


- (IBAction)cancel:(id)sender {

    isSeleted = NO;

    self.tableViewBottom.constant = 0;

     [sender setTitle:@"删除" forState:UIControlStateNormal];

    

    self.bottomView.hidden = YES;

    [self ChangeAlldataForSeleted:NO isMoved:NO];

}

//全选按钮

- (IBAction)allSeleted:(UIButton *)sender {

    //将所有的数据模型存在 一个数组

    sender.selected = !sender.selected;

    if (sender.selected) {

        count = self.dataArray.count;

    }else{

        count = 0 ;

    }

    [self ChangeAlldataForSeleted:sender.selected isMoved:YES];

}


#pragma mark UITableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return self.dataArray.count;

}


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

{

    return 108;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 0.000000001;

}

// cell 可以用通一个cell  可以给不同的标识符 这样就可以区分 避免重用机制

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

    NSString * cellIdentifier = @"My_MyCommodityTableViewCell";

    My_CommodityModel * model = self.dataArray[indexPath.row];

    //用是否变换另一种cell

    if (model.isMoved ==YES) {

        cellIdentifier = @"My_MyCommodityTableViewCellSelected";

    }else{

        cellIdentifier = @"My_MyCommodityTableViewCell";

    }

        My_MyCommodityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

        //cell 是否被选中

        if (model.seleted == YES) {

            cell.selectedImageView.image = [UIImage imageNamed:@"s_selected.png"];

        }else{

            cell.selectedImageView.image = [UIImage imageNamed:@"s_nomol.png"];

        }

        return cell;

}


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

    

        //在这里访问点击 商品详情的接口

        if (isSeleted == NO) {

            NSLog(@"商品详情");

        }else{

            My_MyCommodityTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            My_CommodityModel * model = self.dataArray[indexPath.row];

            model.seleted = !model.seleted;

            //cell 是否被选中

            if (model.seleted == YES) {

                count ++;

                cell.selectedImageView.image = [UIImage imageNamed:@"s_selected.png"];

            }else{

                count --;

                cell.selectedImageView.image = [UIImage imageNamed:@"s_nomol.png"];

            }

        }

       //点击cell 选中状态的个数  用来判断全选按钮的状态

    if (count == self.dataArray.count) {

        self.allSeletedButton.selected = YES;

    }else{

        self.allSeletedButton.selected = NO;

    }

}



-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

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

{

    //删除状态下 删除那一行

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self deleteTableViewCellForIndexPath:indexPath];

    }

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (isSeleted == YES ) {

        return UITableViewCellEditingStyleNone;

    }

    return UITableViewCellEditingStyleDelete;

}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath

{

    return @"删除";

}

#pragma mark deleteTableViewCell

-(void)deleteTableViewCellForIndexPath:(NSIndexPath *)indexPath

{

    //删除数据

    [self.dataArray removeObjectAtIndex:indexPath.row];

    //删除行

    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}


0 0
原创粉丝点击