--tableview single-Choice implementation of iOS development

Source: Internet
Author: User

The implementation of the idea is relatively simple, here only to record:

Directly on the code:

1, implement Didselectrowatindexpath method

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{    [[ Nsuserdefaults Standarduserdefaults]setvalue:[array ObjectAtIndex:indexPath.row] forkey:app_changevoice];        [_sexttableview Reloaddata];    [TableView Deselectrowatindexpath:indexpath animated:yes];    }

Implement the method inside the Cellforrowatindexpath

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *celltableidentifier =@"Celltableidentifier"; Hpublickcell*cell =[TableView Cellforrowatindexpath:indexpath]; if(Cell = =Nil) {Cell=[[Hpublickcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:celltableidentifier]; Cell.selectionstyle=Uitableviewcellselectionstylenone; } Cell.textsLabel.text=Array[indexpath.row]; Cell.selectionstyle=Uitableviewcellselectionstylegray; //Select storage for State    if([[[[ nsuserdefaults Standarduserdefaults]valueforkey:app_changevoice] Isequaltostring:[array objectAtIndex: Indexpath.row]]) {Cell.accessorytype=Uitableviewcellaccessorycheckmark; }    Else{Cell.accessorytype=Uitableviewcellaccessorynone; }        returncell; }

The array in this is the data source arrays. As follows:

2, above this is the system's selected style, the following is a custom:

The code is as follows:

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Shippingaddresscell*cell = [TableView dequeuereusablecellwithidentifier:@"Shippingaddresscell"]; if(!cell) {Cell= [[Shippingaddresscell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:@"Shippingaddresscell"]; }        if(Self.lastindexpath = =Indexpath) {Cell.selectedImg.image= [UIImage imagenamed:@"clicked"]; }Else{cell.selectedImg.image= [UIImage imagenamed:@"Unclick"]; }        returncell;}-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//previously selected, deselectShippingaddresscell *celled =[TableView Cellforrowatindexpath:_lastindexpath]; Celled.selectedImg.image= [UIImage imagenamed:@"Unclick"]; //record the currently selected position index_lastindexpath =Indexpath; //Tick of the current selectionShippingaddresscell *cell =[TableView Cellforrowatindexpath:indexpath]; Cell.selectedImg.image= [UIImage imagenamed:@"clicked"];}

This can be achieved, as follows:

Multi-choice of free and perfect! Code can be pasted directly to use!

--tableview single-Choice implementation of iOS development

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.