UITableView多選全選,uitableview多選

來源:互聯網
上載者:User

UITableView多選全選,uitableview多選

自訂cell和取到相應的cell就行了

TableViewCell.h

#import <UIKit/UIKit.h>@interface TableViewCell : UITableViewCell {    BOOL _checked;    UIImageView *_checkedImage;}- (void)setChecked:(BOOL)checked;@end

TableViewCell.m

#import "TableViewCell.h"@implementation TableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        _checkedImage = [[UIImageView alloc]init];        _checkedImage.image = [UIImage imageNamed:@"Unselected"];        [self.contentView addSubview:_checkedImage];    }    return self;}- (void)layoutSubviews {    [super layoutSubviews];    _checkedImage.frame = CGRectMake(10, 10, 29, 29);}- (void)setChecked:(BOOL)checked {    if (checked) {        _checkedImage.image = [UIImage imageNamed:@"Selected"];    }else {        _checkedImage.image = [UIImage imageNamed:@"Unselected"];    }
  _checked = checked;}- (void)awakeFromNib { // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end

ViewController.m

#import "ViewController.h"#import "TableViewCell.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {    UITableView *_tableView;}@property (nonatomic,strong)NSMutableArray *array;@property (nonatomic,strong)NSMutableArray *checkedArray;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    [self initDataSource];    self.view.backgroundColor = [UIColor lightGrayColor];    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];    _tableView.dataSource = self;    _tableView.delegate = self;    [self.view addSubview:_tableView];        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    [button setTitle:@"全選" forState:UIControlStateNormal];    [button setTitle:@"取消" forState:UIControlStateSelected];    button.frame = CGRectMake(10, 10, 100, 50);    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    }- (void)initDataSource {    _checkedArray = [NSMutableArray array];    for (int i = 0; i < self.array.count; i ++) {        NSMutableDictionary *dic = [NSMutableDictionary dictionary];        [dic setValue:@"NO" forKey:@"checked"];        [_checkedArray addObject:dic];    }}#pragma mark - 懶載入- (NSMutableArray *)array {    if (!_array) {        _array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];    }    return _array;}#pragma mark - 事件監聽- (void)buttonPressed:(UIButton *)sender {    sender.selected = !sender.selected;    NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[_tableView indexPathsForVisibleRows]];    for (int i = 0; i < [anArrayOfIndexPath count]; i++) {        NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];        //取得對應的cell        TableViewCell *cell = (TableViewCell*)[_tableView cellForRowAtIndexPath:indexPath];        NSUInteger row = [indexPath row];        NSMutableDictionary *dic = [_checkedArray objectAtIndex:row];        if (sender.selected) {            [dic setObject:@"YES" forKey:@"checked"];            [cell setChecked:YES];        }else {            [dic setObject:@"NO" forKey:@"checked"];            [cell setChecked:NO];        }    }}#pragma mark - <UITableViewDataSource,UITableViewDelegate>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return self.array.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *cellID = @"cellID";    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];    if (!cell) {        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];    }    NSUInteger row = indexPath.row;    [self cellChecked:cell row:row isSelected:NO];    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    [tableView deselectRowAtIndexPath:indexPath animated:YES];    TableViewCell *cell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath];    NSUInteger row = indexPath.row;    [self cellChecked:cell row:row isSelected:YES];}#pragma mark - others/** *  點擊,和載入cell的時候進行判斷,從而改變cell的選中狀態 * *  @param cell       自訂的cell *  @param row        tableView的下標 *  @param isSelected 是否是點擊 */- (void)cellChecked:(TableViewCell *)cell row:(NSUInteger)row isSelected:(BOOL)isSelected{        NSMutableDictionary *dic = [_checkedArray objectAtIndex:row];    if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {        if (isSelected) {            [dic setObject:@"YES" forKey:@"checked"];            [cell setChecked:YES];        }else {            [dic setObject:@"NO" forKey:@"checked"];            [cell setChecked:NO];        }    }else {        if (!isSelected) {            [dic setObject:@"YES" forKey:@"checked"];            [cell setChecked:YES];        }else {            [dic setObject:@"NO" forKey:@"checked"];            [cell setChecked:NO];        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.