ios - UITableViewCell not displaying text correctly -
i have list of data search items , select 1 of them expand selected cell. when clear out search text in search bar, wish display original list. able display items in original list, except cell selected during search doesn't updated. attaching snapshots , code better understanding:
notice row #3 in first image has text "a.b. road" whereas same row in third image uses same cell in second image (it should updated "a.b. road") using custom cell , tried creating new cell everytime instead of reusing existing cell. didn't help.
code:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { nsstring *celltext = [self.branchlist objectatindex:[indexpath row]]; uifont *cellfont = [uifont fontwithname:@"helvetica" size:17.0]; cgsize labelsize = [celltext boundingrectwithsize:cgsizemake(tableview.frame.size.width, maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:cellfont} context:nil].size; cgfloat cellheight = labelsize.height + 20; return [self.expandedcells containsobject:indexpath] ? cellheight * 5 : cellheight; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *statelistcellid = @"statelist"; branchdetailstableviewcell *statelistcell = [tableview dequeuereusablecellwithidentifier:statelistcellid]; if (!statelistcell) { [tableview registernib:[uinib nibwithnibname:@"branchdetailstableviewcell" bundle:nil] forcellreuseidentifier:statelistcellid]; statelistcell = [tableview dequeuereusablecellwithidentifier:statelistcellid]; } return statelistcell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { if ([self.expandedcells containsobject:indexpath]) { [self.expandedcells removeobject:indexpath]; [self.tableview beginupdates]; [self.tableview reloadrowsatindexpaths:@[self.selectedindexpath] withrowanimation:uitableviewrowanimationnone]; [self.tableview endupdates]; } else { [self.activityindicator showactivityindicatorforview:self.navigationcontroller.view]; self.selectedindexpath = indexpath; [self.expandedcells addobject:indexpath]; [self getdatafromservice]; } } - (void)tableview:(uitableview *)tableview willdisplaycell:(branchdetailstableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath { cell.accessorytype = uitableviewcellaccessorydisclosureindicator; [self displaydataonthecell]; } - (void)searchbarcancelbuttonclicked:(uisearchbar *)searchbar { if ([self.tableview numberofrowsinsection:0] != [self.branchlistcopy count]) { [self.expandedcells removeallobjects]; // copy original list display. self.branchlist = self.branchlistcopy; [self.tableview reloaddata]; } }
it looks it's cell not getting rendered again because when debug, see "a.b. road" in array, it's cell not displaying it. tried calling "[cell setneedsdisplay]" , creating new cell instead of reusing, nothing helped. else help?
thanks!
i able work replacing below code [tableview reloaddata]
in didselectrowatindexpath method keeping rest of code is.
[self.tableview beginupdates]; [self.tableview reloadrowsatindexpaths:@[self.selectedindexpath] withrowanimation:uitableviewrowanimationnone]; [self.tableview endupdates];
final working solution:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { nsstring *celltext = [self.branchlist objectatindex:[indexpath row]]; uifont *cellfont = [uifont fontwithname:@"helvetica" size:17.0]; cgsize labelsize = [celltext boundingrectwithsize:cgsizemake(tableview.frame.size.width, maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:cellfont} context:nil].size; cgfloat cellheight = labelsize.height + 20; return [self.expandedcells containsobject:indexpath] ? cellheight * 5 : cellheight; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *statelistcellid = @"statelist"; branchdetailstableviewcell *statelistcell = [tableview dequeuereusablecellwithidentifier:statelistcellid]; if (!statelistcell) { [tableview registernib:[uinib nibwithnibname:@"branchdetailstableviewcell" bundle:nil] forcellreuseidentifier:statelistcellid]; statelistcell = [tableview dequeuereusablecellwithidentifier:statelistcellid]; } return statelistcell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { if ([self.expandedcells containsobject:indexpath]) { [self.expandedcells removeobject:indexpath]; [self.tableview reloaddata]; } else { [self.activityindicator showactivityindicatorforview:self.navigationcontroller.view]; self.selectedindexpath = indexpath; [self.expandedcells addobject:indexpath]; [self getdatafromservice]; } }
Comments
Post a Comment