UItUItableView 自定義 多選和全選

                         



      這個是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];

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章