Solving the problem of Tableviewcell multiple-selection state disappearing caused by cell multiplexing

Source: Internet
Author: User

In the project we often encounter Tableviewcell, but because the cell multiplexing, resulting in a cell sliding out of the interface and then return, the cell selection will disappear, the specific solution we can use Nsmutableset, Nsmutableset and Nsarray are similar, there are addobject:removeobject: two ways to add and remove data, So when we select the cell, we can add the indexpath of the cell or the tag value of the controls on the cell to the Nsmutableset, and then pass the containsobject when the cell is reused: This method is used to determine whether the cell is assigned a selected state. The following is an approximate implementation.

#pragma mark– returns what each row displays (what cell is shown in each row)

– (UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath{

Shoppingcarttableviewcell *cell = (Shoppingcarttableviewcell *) [TableView dequeuereusablecellwithidentifier:@] Shoppingcarttableviewcell "];

if (cell = = nil) {

cell = [[[[NSBundle Mainbundle] loadnibnamed:@ "Shoppingcarttableviewcell" owner:self Options:nil] lastObject];

Cell.selectionstyle = Uitableviewcellselectionstylenone;

Cell.numberTextField.text = @ "111″;

}

Goodcartinfomodel *model = Dataarray[indexpath.row];

[Cell Addthevalue:model];

Cell.delegate = self;

Cell.selectButton.tag = + Indexpath.row;

cell.selectButton.selected = NO;

[Cell.selectbutton addtarget:self Action: @selector (Selectbuttonclick:) forControlEvents: ( UIControlEventTouchUpInside)];

if ([Btnstatusset containsobject:[nsstring stringwithformat:@ "%d", (+ Indexpath.row)]) {

cell.selectButton.selected = YES;

[Cell.selectbutton setimage:[uiimage imagenamed:@ "Btn_select_default"] forstate: (UIControlStateNormal)];

}

return cell;

}

-(void) Selectbuttonclick: (UIButton *) btn

{

if (btn.selected = NO) {

btn.selected = YES;

[Btnstatusset addobject:[nsstring stringwithformat:@ "%d", Btn.tag]];

}else

{

btn.selected = NO;

[Btnstatusset removeobject:[nsstring stringwithformat:@ "%d", Btn.tag]];

}

}

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.