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]];
}
}