ios - Why are my cell subviews frames changing when scrolling UICollectionView -
basically, have uicollectionviewcell loads values datasource in addition supposed set height of subview frame equal 1 of values in datasource.
the problem having looks correct when first run project, when scroll few times , forth, cells being reused, text values stay same frame of subview changes.
what confuses me variable setting text in label in cell same variable setting height value subview in same cell; label text correct, yet height of uiview frame changes scrolling.
i know it's how cells being reused, can't put finger on it.
below code cellforrowatindexpath. thanks!
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath; { dailycell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"daycell" forindexpath:indexpath]; cell.backgroundcontainerview.backgroundcolor = [uicolor colorwithred:0.0f green:0.0f blue:0.0f alpha:0.3f]; float total = [[dummydata objectatindex:indexpath.row][@"total"] floatvalue] * 2; uiview * backgroundfillview = [uiview new]; if (![cell viewwithtag:1000]) { nslog(@"creating backgroundfillview on cell: %ld", (long)indexpath.row); backgroundfillview.tag = 1000; [cell.backgroundcontainerview addsubview:backgroundfillview]; } cell.debugcellnumber.text = [nsstring stringwithformat:@"%ld", (long)indexpath.row]; cell.debugcelltotal.text = [nsstring stringwithformat:@"%f", total]; backgroundfillview.backgroundcolor = [uicolor colorwithred:0.0f green:0.0f blue:0.0f alpha:0.5f]; backgroundfillview.frame = cgrectmake(0, 200 - total, 60, total); nslog(@"cell: %ld, total: %f", (long)indexpath.row, total); nslog(@"cell: %ld, backgroundfillview height: %f", indexpath.row, backgroundfillview.frame.size.height); nslog(@"cell: %ld, backgroundfillview y: %f", indexpath.row, backgroundfillview.frame.origin.y); return cell; }
you add backgroundfillview
first time populate cell. not when re-used.
replace:
uiview * backgroundfillview = [uiview new]; if (![cell viewwithtag:1000]) { nslog(@"creating backgroundfillview on cell: %ld", (long)indexpath.row); backgroundfillview.tag = 1000; [cell.backgroundcontainerview addsubview:backgroundfillview]; }
with:
uiview * backgroundfillview = [cell viewwithtag:1000]; if (! backgroundfillview) { nslog(@"creating backgroundfillview on cell: %ld", (long)indexpath.row); backgroundfillview = [uiview new]; backgroundfillview.tag = 1000; [cell.backgroundcontainerview addsubview:backgroundfillview]; }
Comments
Post a Comment