ios - Custom UITableViewCell functions are called, but native table is presented -
custom uitableviewcell functions called, native table presented
this code in init function:
self.answerstable = [[uitableview alloc]initwithframe:cgrectmake(0, 45, 280, 500)]; self.answerstable.datasource = self; self.answerstable.delegate = self;
this code in viewdidload function:
[self.answerstable registerclass:[answertableviewcell class] forcellreuseidentifier:cellidentifier];
this table delegate functions, called, , cell not nil in end of it:
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [self.question.answers count]; } -(answertableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { answertableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[answertableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } answerobject* answer = self.question.answers[indexpath.row]; [cell setupanswertableviewcell:self.question answer:answer row:indexpath.row]; return cell; }
and here answertableviewcell:
#import "answertableviewcell.h" @implementation answertableviewcell - (id)init { self = [super init]; if (self) { self.frame = cgrectmake(0, 0, 280, 77); self.answerlabel = [[uilabel alloc]initwithframe:cgrectmake(0, 0, self.frame.size.width-20, self.frame.size.height)]; self.answerlabel.numberoflines = 0; self.answerlabel.textcolor = [uicolor whitecolor]; self.answerlabel.font = [uifont fontwithname:@"helveticaneue-medium" size:20]; self.answertoggle = [[uibutton alloc]initwithframe:cgrectmake(self.frame.size.width-50, 23, 30, 30)]; self.backgroundimage = [[uiimageview alloc]initwithframe:self.frame]; [self addsubview:self.answerlabel]; [self addsubview:self.answertoggle]; [self addsubview:self.backgroundimage]; } return self; } - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { self.frame = cgrectmake(0, 0, 280, 77); self.answerlabel = [[uilabel alloc]initwithframe:cgrectmake(0, 0, self.frame.size.width-20, self.frame.size.height)]; self.answerlabel.numberoflines = 0; self.answerlabel.textcolor = [uicolor whitecolor]; self.answerlabel.font = [uifont fontwithname:@"helveticaneue-medium" size:20]; self.answertoggle = [[uibutton alloc]initwithframe:cgrectmake(self.frame.size.width-50, 23, 30, 30)]; self.backgroundimage = [[uiimageview alloc]initwithframe:self.frame]; [self addsubview:self.answerlabel]; [self addsubview:self.answertoggle]; [self addsubview:self.backgroundimage]; } return self; } -(void)setupanswertableviewcell:(questionobject*)question answer:(answerobject*)answer row:(nsinteger)row{ self.question = question; self.answer = answer; self.row = row; self.answerlabel.text = answer.answertext; [self.answertoggle addtarget:self action:@selector(flip:) forcontrolevents:uicontroleventtouchdown]; self.answertoggle.tag = [answer.answerid intvalue]; if (self.row == 0) { self.backgroundimage.image = [uiimage imagenamed:@"list_top_item_not_selected_612x113px.png"]; } else if (self.row == ([self.question.answers count] - 1)){ self.backgroundimage.image = [uiimage imagenamed:@"list_bottom_item_not_selected_612x113px.png"]; } else{ self.backgroundimage.image = [uiimage imagenamed:@"list_item_not_selected_612x113px.png"]; } }
so, whats wrong?
the problem is:
cell = [[answertableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];
specifically: uitableviewcellstyledefault
you're asking create cell default style. either use [[answertableviewcell alloc] init];
, set reuse identifier. or create own init takes in reuse identifier without style param
edit:
after cell code added believe issue using init function add ui elements. these being overridden cells uiview
contentview
. contentview
need customise in order add these elements.
this article walk through how , using drawrect
method customise uiview
.
http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/
Comments
Post a Comment