How do I make multiple selections in iOS?
The basic idea is to set a flag bit for the data bound by the cell,
To identify whether the cell is selected,
If selected, modify the flag bit to Yes,
and add the selected rows to the variable array
If the flag bit is yes when checked, it indicates that it is selected
Then you need to change the flag bit to No
And remove from the selected array
Implementation process, refer to the previous single implementation article: http://blog.csdn.net/yangbingbinga/article/details/47057285:
(1) Create the relevant Tableviewcontroller
Using the system's cell
Data with the Person class mapping cell
Also includes whether the selected identity bit
(2) Key code
//
Tableviewcontroller2.m
app39-Table View 8-Radio Check
//
Created by Mrbean on 15/7/24.
Copyright (c) 2015 Yangbin. All rights reserved.
#import "TableViewController2.h" #import "Person.h" @interface TableViewController2 () @property (strong,nonatomic) Nsmutablearray *marr, @property (strong,nonatomic) Nsmutablearray *selectrows; @end
@implementation TableViewController2
-(void) viewdidload { [super viewdidload]; _marr = [[Nsmutablearray alloc]init]; _selectrows = [Nsmutablearray new]; for (int i=0; i<20; i++)//Generate a large amount of false data {person *p = [person alloc]init]; P.title = [NSString stringwithformat:@ "%ititle", I]; P.detail = [NSString stringwithformat:@ "%idetail", I]; p.ifselected = NO; [_marr addobject:p]; } }
-(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.}
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView { return 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section { return _marr.count;}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Acell" Forindexpath:indexpath]; Person *p = _marr[indexpath.row]; Cell.textLabel.text = P.title; Cell.detailTextLabel.text = P.detail; if (p.ifselected) cell.accessorytype = Uitableviewcellaccessorycheckmark; else Cell.accessorytype = Uitableviewcellaccessorynone; return cell;}
Change data When selected
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{person *p = _marr[ Indexpath.row]; if (p.ifselected)//If the row is already selected, the second time is to cancel the row selected { p.ifselected = no;//unchecked [_selectrows removeobject:@ ( Indexpath.row)];//is removed from the heavy array } else { p.ifselected = yes;//selected [_selectrows addobject:@ ( Indexpath.row)]; } [TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];//Refresh the line // Added to the array, this operation may result in duplicate data}
-(Ibaction) Tapsubmit: (Uibarbuttonitem *) sender{ nsmutablestring *mstr = [[nsmutablestring alloc]initwithstring: @ "The number of rows you have selected is:"]; [_selectrows enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) { [mstr appendformat:@ "%@,", obj]; }]; Uialertview *alert = [[Uialertview alloc]initwithtitle:mstr message:nil delegate:nil cancelButtonTitle:@ "I know." Otherbuttontitles:nil, nil]; [Alert show];}
@end
Single-Radio implementation: http://blog.csdn.net/yangbingbinga/article/details/47057285
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-uitableviewcontroller Check/multi-select implementation 2