iphone - Check image not working properly in uitableviewcell over button, tag and cell reuse issue -
check images removing after scrolling.
have reuse cell , dont want use didselectrow method because having several buttons in 1 cell , sample created stackoverflow.
searched lot in google , found helpless.
here sample code
// // tvviewcontroller.h // tableviewcheck #import <uikit/uikit.h> @interface tvviewcontroller : uiviewcontroller<uitableviewdatasource,uitableviewdelegate>{ uitableview *sampletableview; uiimageview * imageview1; uibutton* abutton1; uiimageview *imageviewchk1; int tag; } @property (nonatomic, strong) iboutlet uitableview *sampletableview; @end // tvviewcontroller.m // tableviewcheck #import "tvviewcontroller.h" @interface tvviewcontroller () @end @implementation tvviewcontroller @synthesize sampletableview; - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ // return number of rows in section. // number of items in array (the 1 holds list) // nslog(@"selected %i , deliveryarray %i",[queuearray count],[deliveryarray count]); return 50; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ //where configure cell in each row // nsstring *cellidentifier =[nsstring stringwithformat:@"%i-%i", indexpath.section,indexpath.row]; static nsstring *cellidentifier = @"cell"; uitableviewcell *cell; cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]autorelease]; [cell setselectionstyle:uitableviewcellselectionstylenone]; } imageviewchk1 = [[uiimageview alloc] initwithframe:cgrectmake(56,0 , 24,24)]; imageviewchk1.image = [uiimage imagenamed:@"check.png"]; imageviewchk1.tag=indexpath.row+10000; imageviewchk1.hidden=yes; imageview1 = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, 80,80)]; imageview1.image = [uiimage imagenamed:@"newframe.png"]; abutton1 = [uibutton buttonwithtype:uibuttontyperoundedrect]; [abutton1 settag:indexpath.row]; [abutton1 setimage:imageview1.image forstate:uicontrolstatenormal]; abutton1.frame = cgrectmake(0, 0, 80,80); [abutton1 addtarget:self action:@selector(buttonclicked:) forcontrolevents:uicontroleventtouchupinside]; [abutton1 addsubview:imageviewchk1]; [cell.contentview addsubview:abutton1]; return cell; } -(void)buttonclicked:(uibutton*)sender { tag = sender.tag; nslog(@"buttonclicked %i",tag); uiimageview *imageview = (uiimageview *)[sampletableview viewwithtag:tag+10000]; if(imageview.hidden) { imageview.hidden=no; } else { imageview.hidden=yes; } }
well, problem implementation is, set imageview.hidden property in buttonclicked method.
when cell not shown on screen moment , shown again, cellforrow called again.
the problem here is, not store in cell imageview.hidden has been set no. why every time cell shown again, cellforrow: sets imageview.hidden yes.
so solution store numbers of cell, imageview.hidden= no , check in cellforrow if cell @ indexpath 1 imageview.hidden= no.
Comments
Post a Comment