IOS TableView implements the single-select Checkmark feature

Source: Internet
Author: User

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

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.