ios - UITableView won't present -
i have strange situation in app , can't explain or locate bug. have uiviewcontroller tableview in it. in table view have 3 prototype cells, have 2 section divided so:
first section: row 0 (cell id: episodescrollerscell) second section: row 0 (cell id: addcommentscell) : row 1+ (cell id: commentcell)
the required methods in protocol listed below.
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { nsinteger rownum; if(section == 0){ rownum = 1; }else{ rownum = 1; // here problem. if change number of row above 1 } return rownum; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier; if(indexpath.section == 0){ cellidentifier = episodescrollerscell; }else if (indexpath.section == 1 && indexpath.row == 0){ cellidentifier = addcommentscell; }else{ cellidentifier = commentcell; } uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; return cell; }
now problem arises when want have 2 rows or above in second section (i.e have 2 prototype cells in 1 section), view controller won't show. i've logged cellforrowatindexpath: method see if cells gets loaded , do.
any advice? thanks,
what seem doing trying dequeue reusable cell chance no cell has been created before. however, if no cells have been created/allocated, dequeue return nil
value. tells program given indexpath
there no cell displayed.
so, if nil value dequeue function, table try create new cell given cell identifier. so, suggest following:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier; if(indexpath.section == 0){ cellidentifier = episodescrollerscell; }else if (indexpath.section == 1 && indexpath.row == 0){ cellidentifier = addcommentscell; }else{ cellidentifier = commentcell; } uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if(!cell){ if(indexpath.section == 0){ //initialise new cell here, through nib or code //for eg. cell = [[uitableviewcell alloc] initwithstyle: ...]; } //do specific initialisation each type of cell need here } return cell; }
this way, when dequeue function returns nil
value, program automatically creates new cell fit in place.
hope helps.
Comments
Post a Comment