ios - Cells disappear when scroll down in my UITableView -
when scroll down in tableview contents of cells disappear (labels , imageviews). code:
-(void)viewwillappear:(bool)animated{ [comentarios removeallobjects]; nsstring *lookup=[nsstring stringwithformat:@"http://my.url"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:lookup]]; [request sethttpmethod:@"get"]; nserror *error = nil; nsurlresponse *response = nil; nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nsmutablearray *jsondict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]; nslog(@"%@",jsondict); (int i=0; i<[jsondict count]; i++) { comentario *come=[[comentario alloc] init]; come.nick=[[jsondict objectatindex:i] objectforkey:@"nick"]; come.comment=[[jsondict objectatindex:i] objectforkey:@"content"]; come.avatar=[[jsondict objectatindex:i] objectforkey:@"color"]; [comentarios addobject:come]; } [self reloadinputviews]; [self.comentariostableview reloaddata]; } and
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; // configure cell... if( cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier: cellidentifier]; } // display recipe in table cell uiimageview *avatar = (uiimageview *)[cell viewwithtag:100]; avatar.image = [uiimage imagenamed:[[comentarios objectatindex:indexpath.row] avatar]]; uilabel *nick = (uilabel *)[cell viewwithtag:101]; nick.text =[[comentarios objectatindex:indexpath.row] nick]; uilabel *comment = (uilabel *)[cell viewwithtag:102]; comment.text = [[comentarios objectatindex:indexpath.row] comment]; uibutton *sinvoto = (uibutton *)[cell viewwithtag:103]; uibutton *ticket = (uibutton *)[cell viewwithtag:104]; return cell; } i can't see mistake, please me. thank in advance
edit nª1 changed this
viewcontroller.m
#import "viewcontroller.h" #import "simpletablecell.h" @interface viewcontroller (){ nsmutablearray *comentarios; } @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.comenatriotableview.delegate=self; self.comenatriotableview.datasource=self; self.automaticallyadjustsscrollviewinsets = no; uiimage *plus=[[uiimage imagenamed:@"megafono.png"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem alloc] initwithimage:plus style:uibarbuttonitemstyleplain target:self action:@selector(comenta:)]; self.navigationcontroller.navigationbar.bartintcolor=[uicolor colorwithred:204.0/255.0 green:0.0/255.0 blue:00.0/255.0 alpha:1.0f]; comentarios=[[nsmutablearray alloc] init]; [self reloadinputviews]; } -(void)viewwillappear:(bool)animated{ [comentarios removeallobjects]; nsstring *lookup=[nsstring stringwithformat:@"http://myurl"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:lookup]]; [request sethttpmethod:@"get"]; nserror *error = nil; nsurlresponse *response = nil; nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nsmutablearray *jsondict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]; nslog(@"%@",jsondict); (int i=0; i<[jsondict count]; i++) { comentario *come=[[comentario alloc] init]; come.nick=[[jsondict objectatindex:i] objectforkey:@"nick"]; come.comment=[[jsondict objectatindex:i] objectforkey:@"content"]; come.avatar=[[jsondict objectatindex:i] objectforkey:@"color"]; [comentarios addobject:come]; } [self reloadinputviews]; } -(void)viewdidappear:(bool)animated{ } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ return [comentarios count]; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 110; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *simpletableidentifier = @"simpletablecell"; simpletablecell *cell = [tableview dequeuereusablecellwithidentifier:simpletableidentifier forindexpath:indexpath]; comentario *comentario=[[comentario alloc] init]; comentario =[comentarios objectatindex:indexpath.row]; cell.avatar.image=[uiimage imagenamed:[comentario avatar]]; cell.nick.text=[comentario nick]; cell.comment.text =[comentario comment]; return cell; cell.selectionstyle = uitableviewcellselectionstylenone; } -(void)comenta:(id)sender{ [self performseguewithidentifier:@"gocomment" sender:self]; } @end and viewcontroller.h
#import <uikit/uikit.h> #import "comentario.h" @interface viewcontroller : uiviewcontroller<uitableviewdatasource,uitableviewdelegate> @property (weak, nonatomic) iboutlet uitableview *comenatriotableview; @end edit nª3 proble when scroll down, information of cells become nil comentarios array have information.
edit nª4 here project https://github.com/quimerakoke/bang-
i have couple of other suggestions improve code.
you have call super in viewdidappear , viewwillappear methods:
-(void)viewdidappear:(bool)animated{ [super viewdidappear:yes]; } instead of using:
comentario *comentario = [[comentario alloc] init]; comentario = [comentarios objectatindex:indexpath.row]; with:
comentario *comentario = [comentarios objectatindex:indexpath.row]; finally, should check datasource:
for (int i=0; i<[jsondict count]; i++) { comentario *come = [[comentario alloc] init]; come.nick = [[jsondict objectatindex:i] objectforkey:@"nick"]; come.comment = [[jsondict objectatindex:i] objectforkey:@"content"]; come.avatar = [[jsondict objectatindex:i] objectforkey:@"color"]; [comentarios addobject:come]; nslog(@"nick = %@, comment = %@, avatar = %@", come.nick, come.comment, come.avatar); } edit:
instead of using:
@interface comentario : nsobject @property (weak,nonatomic) nsstring *nick; @property (weak,nonatomic) nsstring *comment; @property (weak,nonatomic) nsstring *avatar; @end you should use:
@interface comentario : nsobject @property (copy,nonatomic) nsstring *nick; @property (copy,nonatomic) nsstring *comment; @property (copy,nonatomic) nsstring *avatar; @en your problem has been resolved.
copy
copy required when object mutable. use if need value of object @ moment, , don't want value reflect changes made other owners of object. need release object when finished because retaining copy.
weak
weak similar strong except won't increase reference count 1. not become owner of object holds reference it. if object's reference count drops 0, though may still pointing here, deallocated memory.
this website learn strong , weak ios 5. http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
in addition above problem,your constrains of simpletablecell incorrect:

you should go main.storyboard , check it.(in interface builder select compact width , compact height size class)
Comments
Post a Comment