I believe that when you do the project has encountered the need to implement this function---to achieve a single selection of a cell indicates the selected
The implementation of this feature only requires code in two methods
Preferred we expose a property
@property(nonatomic, strong)Nsindexpath *lastpath, and to its synthesize
is primarily used to receive the Indexpath of the user's last selected cell
The first step: Implement the following code in the Cellforrowatindexpath: method
-(UITableViewCell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (nsindexpath *) indexpath{
Nsinteger row = [Indexpath row];
Nsinteger oldrow = [lastpath row];
if (row = = Oldrow && lastpath!=nil) {
Cell. accessorytype = Uitableviewcellaccessorycheckmark;
}else{
Cell. accessorytype = Uitableviewcellaccessorynone;
}
}
Step Two: Implement the following code in Didselectrowatindexpath:
-(void) TableView: (uitableview *) TableView Didselectrowatindexpath: (nsindexpath *) indexpath{
int newRow = [Indexpath row];
int oldrow = (lastpath! = nil)? [ Lastpath row]:-1;
if (newRow! = Oldrow) {
UITableViewCell *newcell = [TableView cellforrowatindexpath:indexpath];
Newcell. accessorytype = Uitableviewcellaccessorycheckmark;
UITableViewCell *oldcell = [TableView cellforrowatindexpath:Lastpath];
Oldcell. accessorytype = Uitableviewcellaccessorynone;
Lastpath = Indexpath;
}
[TableView deselectrowatindexpath: Indexpath animated:YES];
}
Ok, it's time to call it off, so the effect is that each click of a cell will make a selected flag and the checkmark will not be reused when the table view is moved.
Hope to be helpful to beginners!
IOS TableView implements the single-select Checkmark feature