iOS: UITableView cell.tag number keeps increasing when in and out of the View -
how can keep track if cell assigned tag number? i'll have anywhere 10 - 90 cells created. however, when scroll , down (in , out-of-view), after cells recreated, cell tag number continue increase.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; nsmanagedobject *managedobject = rowmodels[indexpath.row]; nsstring *entityname= [[managedobject entity]name]; cell.textlabel.text = [nsstring stringwithformat:@"%@ %i", entityname, [indexpath row]]; cell.tag = celltagindex++; nsstring *tagstring = [nsstring stringwithformat:@"%d", cell.tag]; //celltoobjectmap nsmutabledictionary if ([entityname isequaltostring:@"test1" ]) { [celltoobjectmap setvalue:[managedobject valueforkey:@"test1_id"] forkey:tagstring]; } else if ([entityname isequaltostring:@"test2t" ]) { [celltoobjectmap setvalue:[managedobject valueforkey:@"test2_id"] forkey:tagstring]; } nslog(@"cell.tag %@ value: %@", tagstring, [celltoobjectmap objectforkey:tagstring]); return cell; }
log:
2013-08-15 19:07:57.243 time[8510:c07] cell.tag 0 value: 95ab73f8-e0c2-4d17-b6be-9fc2e696959d 2013-08-15 19:07:57.244 time[8510:c07] cell.tag 1 value: ea163c15-0cf1-4275-b939-0ed9f62c240d 2013-08-15 19:07:57.245 time[8510:c07] cell.tag 2 value: ddfd36f6-4cad-4c26-a38c-b22d827199bd 2013-08-15 19:07:57.245 time[8510:c07] cell.tag 3 value: ac9fd255-08cf-44c4-b168-e6441d08ac54 2013-08-15 19:07:57.246 time[8510:c07] cell.tag 4 value: 48c2de0c-88c3-48cc-bbb1-350088ee9862 2013-08-15 19:07:57.247 time[8510:c07] cell.tag 5 value: 5c4495a1-ff3d-439e-bd01-88312ef881ad ........ ........ // cell.tag continues increase scroll , down screen. 2013-08-15 19:10:38.903 time[8510:c07] cell.tag 254 value: 81caa228-51f4-464c-8b2b-12d2a66c3ba8 2013-08-15 19:10:38.969 time[8510:c07] cell.tag 255 value: 7dc03657-b987-4994-8066-5393f77d891b 2013-08-15 19:10:39.052 time[8510:c07] cell.tag 256 value: bb1bcfd2-fbd9-46fa-b662-56085254fd46 2013-08-15 19:10:39.169 time[8510:c07] cell.tag 257 value: 0c78ebe0-ad83-4a21-8546-37ea17eab5ca
how can keep track if cell number assigned cell.tag, , if has, how can sure if dictionary has existing key?
if need associate objects in model cells, don't make association between cell directly because cells reused constantly. instead, associate index path.
assuming celltoobjectmap
nsmutabledictionary
, try:
[celltoobjectmap setobject:[managedobject valueforkey:@"test1_id"] forkey:indexpath];
in general, though, should use indexpath
access object directly out of data structures have built represent table.
Comments
Post a Comment