UITableView multiple options: Select All and uitableview multiple options

Source: Internet
Author: User

UITableView multiple options: Select All and uitableview multiple options

You just need to customize the cell and obtain the corresponding 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 lightGr AyColor]; _ 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: @ "select all" forState: UIControlStateNormal]; [button setTitle: @ "cancel" forState: UIContro LStateSelected]; 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: @ "che Cked "]; [_ checkedArray addObject: dic] ;}# pragma mark-lazy loading-(NSMutableArray *) array {if (! _ Array) {_ array = [[NSMutableArray alloc] initWithObjects: @ "1", @ "2", @ "3", @ "4", @ "5 ", nil];} return _ array;} # pragma mark-event listening-(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]; // obtain the corresponding cell TableViewCell * cell = (TableViewCell *) [_ tableView cellForRowAtIndexPath: indexPath]; NSUInteger row = [indexPath row]; NSMutableDictionary * dic = [_ checkedArray o BjectAtIndex: 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) s Ection {return self. array. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellID = @ "cellID"; TableViewCell * cell = [tableView progress: 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 ce LlForRowAtIndexPath: indexPath]; NSUInteger row = indexPath. row; [self cellChecked: cell row: row isSelected: YES] ;}# pragma mark-others/*** click to determine when the cell is loaded, to change the cell selection status ** @ param cell custom cell * @ param row tableView subscript * @ param isSelected whether to click */-(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] ;}}

:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.