Monday 15 March 2010

ios - CustomCell is not reflected in UITableView -



ios - CustomCell is not reflected in UITableView -

i'd utilize customcell in uitableview. in next code, customcell not reflected.

viewcontroller.m

- (void)viewdidload { [super viewdidload]; arr = [nsarray arraywithobjects:@"aaa", @"bbb", @"ccc", @"ddd", @"eee", @"fff", nil]; mytableview = [[uitableview alloc]initwithframe:cgrectmake(20, 130, 280, 220)]; [mytableview registerclass:[customcell class] forcellreuseidentifier:@"cell"]; mytableview.delegate = self; mytableview.datasource = self; [self.view addsubview:mytableview]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring* cellidentifier = @"cell"; customcell* cell = (customcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[customcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } cell.customlabel.text = [arr objectatindex:indexpath.row]; homecoming cell; }

customcell.m

- (id)init{ self = [super init]; if (self){ self.backgroundcolor = [uicolor redcolor]; //but in viewcontroller, cell not red. } homecoming self; }

how prepare output customcell?

in cellforrowatindexpath, initwithstyle:reuseidentifier: called display custom cell have created programatically in customcell.m in tableview next code:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring* cellidentifier = @"cell"; customcell* cell = (customcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { line---> cell = [[customcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } cell.customlabel.text = [arr objectatindex:indexpath.row]; homecoming cell; }

so in customcell.m file need implement initwithstyle:reuseidentifier: , not -init.

customcell.m

- (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // configure control(s) self.backgroundcolor = [uicolor redcolor]; } homecoming self; }

ios objective-c uitableview

No comments:

Post a Comment