ios - UITableView shows wrong cell contents -
i've stucked uitableview , adding cells.
i have chat-app based on uitableview displaying messages. works fine when user opens chat old messages. when user adds new message - cell message populated wrong content.
i use custom cell displaying messages.here code (simplified):
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // im using storyboard, don't need if (!cell) part rightmessagecell *cell = (rightmessagecell *) [self.messagetableview dequeuereusablecellwithidentifier:@"right_text_message"]; [self configurecell:cell forindexpath:indexpath]; return cell; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { [self configurecell:self.offscreencell forindexpath:indexpath]; [self.offscreencell setneedslayout]; [self.offscreencell layoutifneeded]; result = [self.offscreencell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize].height; } - (void) configurecell:(rightmessagecell *) cell forindexpath:(nsindexpath *) indexpath { chatmessagemodel *message = [self.fetchcontroller objectatindexpath:indexpath]; rightmessagecell.messagetextview.text = message.messagetext; }
and frc methods:
#pragma mark - nsfetchedresultscontrollerdelegate - (void)controllerwillchangecontent:(nsfetchedresultscontroller *)controller { [self.messagetableview beginupdates]; } - (void)controller:(nsfetchedresultscontroller *)controller didchangeobject:(id)anobject atindexpath:(nsindexpath *)indexpath forchangetype:(nsfetchedresultschangetype)type newindexpath:(nsindexpath *)newindexpath { switch(type) { case nsfetchedresultschangeinsert: [self.messagetableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationnone]; break; case nsfetchedresultschangedelete: [self.messagetableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationnone]; break; case nsfetchedresultschangeupdate: [self.messagetableview reloadrowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationnone]; break; } } - (void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [self.messagetableview endupdates]; }
but can't figure out why newly created cells comes previous cells content , replace own content when redrawn (for ex when scroll , down)
in uitableviewcell
subclass need implement method:
-(void)prepareforreuse { [super prepareforresuse]; [self.textlabel settext:nil]; // reset layout // reset text on labels // reset other properties such images or text colour }
this called before uitableviewcell reused, chance reset content/layout default.
another area see there's issue cell
vs rightmessagecell
here:
- (void) configurecell:(rightmessagecell *) cell forindexpath:(nsindexpath *) indexpath { chatmessagemodel *message = [self.fetchcontroller objectatindexpath:indexpath]; rightmessagecell.messagetextview.text = message.messagetext; }
your code shouldn't able compile above method.
Comments
Post a Comment