ios - appending rows to tableView from within a block -


i'm trying add/append rows single section of tableview within block. problem when add index paths (insertrowsatindexpaths) code crashes (the number of rows contained in existing section after update (12) must equal number of rows contained in section before update (1) yadayadayada)

now traced fact [weakself.tableview numberofrowsinsection:0] not updated after appended data data source (also using weakself)

anyone knows i'm missing here?

__weak typeof(self) weakself = self;  _tipsearchcontroller.searchresultblock = ^(nsarray *tips, bool firstbatch) {     weakself.nolocationsaftersearchfilters = no;     if (firstbatch==yes) {         [weakself.tips removeallobjects]; // empty datasource if required         [weakself.tableview reloaddata];     }      nsuinteger currenttipcount=weakself.tips.count;     [weakself.tips addobjectsfromarray:tips]; // adding data datasource.      nsmutablearray *indexpaths = [nsmutablearray array];     (nsuinteger = 0; < tips.count; ++i) {         [indexpaths addobject:[nsindexpath indexpathforrow:i+currenttipcount insection:0]];     }      // crash here ;(     [weakself.tableview insertrowsatindexpaths:indexpaths withrowanimation:uitableviewrowanimationnone];  };   - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return self.tips.count; } 

first, ensure searchresultblock gets executed on main thread @mbo42 mentioned. second, wrap rows insertions in beginupdates / endupdates:

[weakself.tableview beginupdates]; nsuinteger currenttipcount=weakself.tips.count; [weakself.tips addobjectsfromarray:tips]; // adding data datasource.  nsmutablearray *indexpaths = [nsmutablearray array]; (nsuinteger = 0; < tips.count; ++i) {     [indexpaths addobject:[nsindexpath indexpathforrow:i+currenttipcount insection:0]]; }  [weakself.tableview insertrowsatindexpaths:indexpaths withrowanimation:uitableviewrowanimationnone]; [weakself.tableview endupdates]; 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -