iphone - Cocoa - UITableViewController strange behaviour ! -
i'm losing mind on this:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *myidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:myidentifier]; uilabel *label; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithframe:cgrectzero reuseidentifier:myidentifier] autorelease]; label=[[uilabel alloc]initwithframe:cgrectmake(20, 20, 200, 30)]; [cell addsubview:label]; [label settext:[nsstring stringwithformat: @"%d",indexpath.row]]; } return cell; } in cells' labels, see numbers instead of progression expecting (0,1,2,3,4...):
0 1 2 0 4 1 2 0 4 1
any idea where's error? seems can't figure out.
edit resolved after putting initialization of label after cell==nil{} block.(don't know why put there in first place.)
you need perform cell configuration outside of if:
uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:myidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithframe:cgrectzero reuseidentifier:myidentifier] autorelease]; uilabel *label=[[uilabel alloc]initwithframe:cgrectmake(20, 20, 200, 30)]; [cell addsubview:label]; [label release]; } [label settext:[nsstring stringwithformat: @"%d",indexpath.row]]; oh - , should release label after adding avoid memory leak :)
Comments
Post a Comment