tableView的多选
#import "TableViewCell.h"
#import "SelectModel.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray; //源数据
@property (nonatomic, strong) NSMutableArray *selectArray; //选中的数据
@end
@implementation ViewController
- (void)viewDidLoad {
self.dataArray = [NSMutableArray array];
self.selectArray = [NSMutableArray array];
[self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"TableViewCellID"];
self.tableView.allowsMultipleSelection = YES; //让tableView支持多选
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellID"];
return cell;
}
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
//选中cell时数据的相关操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SelectModel *model = self.dataArray[indexPath.row];
[self.selectArray addObject:model.account_id];
}
//取消选中cell时数据的相关操作
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
SelectModel *model = self.dataArray[indexPath.row];
[self.selectArray removeObject:model.account_id];
}
@end
@implementation TableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if(selected){ //cell选中时的样式
}else{ //cell取消选中时的样式
}
}
@end
collectionView的多选
#import "CollectionViewCell.h"
#import "SelectModel.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray; //源数据
@property (nonatomic, strong) NSMutableArray *selectArray; //选中的数据
@end
@implementation ViewController
- (void)viewDidLoad {
self.dataArray = [NSMutableArray array];
self.selectArray = [NSMutableArray array];
[self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellReuseIdentifier:@"CollectionViewCellID"];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView.collectionViewLayout = layout;
self.CollectionView.allowsMultipleSelection = YES; //让CollectionView支持多选
self.CollectionView.dataSource = self;
self.CollectionView.delegate = self;
[self.collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataArray.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCellID" forIndexPath:indexPath];
return cell;
}
#pragma matk - UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100, 100);
}
#pragma mark - UICollectionViewDelegate
//选中cell时数据的相关操作
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
SelectModel *model = self.dataArray[indexPath.row];
[self.selectArray addObject:model.account_id];
}
//取消选中cell时数据的相关操作
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
SelectModel *model = self.dataArray[indexPath.row];
[self.selectArray removeObject:model.account_id];
}
@end
@implementation CollectionViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if(selected){ //cell选中时的样式
}else{ //cell取消选中时的样式
}
}
@end
网友评论