ios - Graphical glitches when adding cells and scrolling with UITableView -
i using uitableview display results of series of calculations. when user hits 'calculate', add latest result screen. when add new cell, uitableviewcell
object added array (which indexed tableview:cellforrowatindexpath:
), , use following code add new row displayed on screen:
[thisview beginupdates]; [thisview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation: uitableviewrowanimationfade]; [thisview endupdates];
this results in new cell being displayed. however, want scroll screen down new cell lowermost cell on-screen. use following code:
[thisview scrolltorowatindexpath:newindexpath atscrollposition:uitableviewscrollpositionbottom animated:yes];
this almost works great. however, first time cell added , scrolled to, appears onscreen briefly before vanishing. view scrolls down correct place, cell not there. scrolling view hand until invisible new cell's position offscreen, again, causes cell appear - after behaves normally. happens first time cell added; subsequent cells don't have problem. happens regardless of combination of scrolltorowatindexpath
, insertrowsatindexpath
animation settings.
edit: i've started inserting cells @ second-to-last position of table, rather end, , problem still occurs - when first inserted, cell 'invisible' until goes offscreen , comes on again. causing this, , how can force cell drawn added table?
you're having problems because updating table without updating data model backing it. tables don't know how many rows have nor cells display. depend on datasource , delegate tell them these things. design expects table track them.
insertrowsatindexpaths:
intended used moving existing rows around table, not adding entirely new logical rows. when insert entirely new cell, tableview looses track of how many rows has.
before display new row, first thing should update values returned by:
– numberofsectionsintableview: – tableview:numberofrowsinsection:
... reflect addition of new rows. allow table understand how big is.
then need update cellforrowatindexpath:
return correct cell added row. need reload table.
after you've done that, should able scroll tableview end , have cell display properly.
the important thing remember tables dumb. table holds no data, doesn't know how many sections , rows has or order rows , sections come in. logic data, sections, rows, cells , cell contents comes datasource and/or delegate. when want change table, change datasource and/or delegate , table reflect changes automatically.
edit:
upon rereading parent, see putting actual uitableviewcell objects in data array , have 1 cell each row.
this not how tableviews supposed work , not scale beyond few dozen rows @ most.
tableviews intended illusion allows display logical table has arbitrary high number or rows. end, keeps enough uitableviewcell objects alive cover visually displayed area in ui. default cell height of 44 pixels means tableview never have more 9 cell objects @ time.
instead of eating memory holding cells not displayed, tableview lets delegate dequeue cell has scrolled off screen, repopulate data of logical row , display in new position. done in cellforrowatindexpath:
you need start on here design. data needs kept separate user interface objects. don't want have more cells alive @ time absolutely necessary because memory use balloon , response time degrade. current problem result of unusual design.
when you've done that, can add result row outlined above.
Comments
Post a Comment