iphone - What is wrong with my UITableView cellForRowAtIndex for Single Selection? -



iphone - What is wrong with my UITableView cellForRowAtIndex for Single Selection? -

below code uitableview, when scroll behaves weirdly (too annoying)... problem due reuseidentifier.... dont know how solve..

- (uitableviewcell *)tableview:(uitableview *)tableview1 cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview1 dequeuereusablecellwithidentifier:cellidentifier]; nsinteger imgtag = 1; nsinteger lbltag = 2; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; cell.selectionstyle = uitableviewcellselectionstylenone; uiimageview *imgview = [[uiimageview alloc] initwithframe:cgrectmake(2, 2, 52, 52)]; // image:[uiimage imagenamed:[self.glasstype objectatindex:indexpath.row]]]; imgview.tag = imgtag; [cell.contentview addsubview:imgview]; [imgview release]; uilabel *lblname = [[uilabel alloc] initwithframe:cgrectmake(60, cell.frame.size.height/4, 200, 21)]; // lblname.text = [self.glassname objectatindex:indexpath.row]; lblname.tag = lbltag; [cell addsubview:lblname]; [lblname release]; } nsinteger imgindex = 2; nsinteger lblindex = 3; ((uiimageview *)[cell viewwithtag:imgtag]).image = [[self.glasstype objectatindex:indexpath.row] objectatindex:imgindex]; ((uilabel *)[cell viewwithtag:lbltag]).text = [[self.glassname objectatindex:indexpath.row] objectatindex:lblindex]; homecoming cell; } - (void)tableview:(uitableview *)tableview1 didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview1 cellforrowatindexpath:indexpath]; cell.accessorytype = uitableviewcellaccessorycheckmark; }

how create cell row @ index remains constant when scrolled??? how create single selection in uitableview??

the reply should not add together subviews table cells outside of "if (cell == nil) { ..." clause or added on , on 1 time again same cell when gets re-used.

see reply question more detailed explanation, including code how prepare it:

cellforrowatindexpath memory management

you cannot store state in table cells because scroll offscreen recycled , re-appear @ different index in table. need set array of model objects store state table cells (such accessory type should be). more detailed explanation can found in reply question:

looping through uitableviewcells of uitableview

if prepare how adding subviews cells, , store "ticked" state in array of model objects setting cell.accessorytype (so can restored when cell dequeued), approach row selection otherwise correct.

so set in tableview:cellforrowatindexpath: method, before return cell;:

mymodelobject *object = [self.arrayofmodelobjects objectatindex:indexpath.row]; bool ischecked = object.checked; cell.accessorytype = ischecked? uitableviewcellaccessorycheckmark: uitableviewcellaccessorynone;

and in tableview: didselectrowatindexpath: method, rid of current logic , replace with:

- (void)tableview:(uitableview *)tableview1 didselectrowatindexpath:(nsindexpath *)indexpath (int = 0; < [self.arrayofmodelobjects count]; i++) { mymodelobject *object = [self.arrayofmodelobjects objectatindex:i]; object.checked = (i == indexpath.row); // check 1 tapped } //refresh table update accessory views rows [tableview1 reloaddata]; }

obviously replace arrayofmodelobjects own model implementation. utilize array of nsnumber objects containing bools if don't want create custom class purpose.

iphone objective-c ios uitableview

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -