uitableview - Iphone UITableViewCell CustomCell -
attempting implement "simple" customcell, have normal tableviewcontroller renders fine using normal "default" methods, need implement custom cell uilabel's , uiimage.
so created customcell.h, customcell.m, customcell.xib
the .h
@interface customcell : uitableviewcell <uitextviewdelegate> { iboutlet uiimageview *image; iboutlet uilabel *name; iboutlet uilabel *date; iboutlet uilabel *comment; } @property (retain,nonatomic) iboutlet uiimageview *image; @property (retain,nonatomic) iboutlet uilabel *name; @property (retain,nonatomic) iboutlet uilabel *date; @property (retain,nonatomic) iboutlet uilabel *comment;
and .m
@implementation customcell @synthesize image; @synthesize name; @synthesize date; @synthesize comment; #pragma mark - #pragma mark view lifecycle - (id) initwithcontroller: (controller *) ctnlr { controllerpointer = ctnlr; return(self); } - (void) setimage:(uiimageview*)image { image = image; } - (void) setname:(nsstring*)name { [name retain]; [name.text release]; name.text = name; } - (void) setdate:(nsstring*)date { [date retain]; [date.text release]; date.text = date; } - (void) setcomment:(nsstring*)comment { [comment retain]; [comment.text release]; comment.text = comment; }
anyway, when attempt create these customcells in cellforrowatindexpath (as 1 expect might implemented) left blank screen. missing big... when created .xib file "interface builder" made sure connect "referencing outlets" appropriate labels , images.
so following implied logic of way xcode framework appears work, followed same reasoning (for lack of exact example) no worky... anyway, if there iphone geeks enlighten me... (yes, there no "[something release]" calls, not sure if needed alloc'd. please tell me there's couple calls leaving out, can't more simple right...?
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; customcell *cell = (customcell*)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if(cell == nil) { cell = [[[nsbundle mainbundle] loadnibnamed:@"customcell" owner:self options:nil] lastobject]; } nsuinteger row = [indexpath row]; snsobject *sobj = [snsarray objectatindex:row]; [cell setname:[sobj getusername]]; nsuinteger row = [indexpath row]; snsobject *sobj = [snsarray objectatindex:row]; cell.name = [[uilabel alloc]init]; cell.name.text = [sobj getusername]; cell.date = [[uilabel alloc]init]; cell.date.text = [sobj getdatetime]; cell.comment = [[uilabel alloc]init]; cell.comment.text = [sobj getcommenttext]; cell.image = [[uiimageview alloc]init]; cell.image.image = [sobj getimageurl]; return(cell) }
thanks in advance!
there other issues code beyond mrcrowl mentioned needing "alloc-init" outlets. in particular, line:
cell = [[[nsbundle mainbundle] loadnibnamed:@"customcell" owner:self options:nil] lastobject];
this not typical idiom used load custom tableview cell .xib. first of all, pass "owner:self", means want hook outlet objects in .xib file outlet members in tableviewcontroller object, not intended. second, you're relying on order of objects returned loadnibnamed:owner:options:, while may work today, may not work tomorrow, or on new release of ios.
instead, usual idiom declare outlet entire tableviewcell in tableviewcontroller: (in .h file):
... uitableviewcell *tvcell; ... @property (nonatomic, retain) iboutlet uitableviewcell *tvcell;
then in place of line, have this:
[[nsbundle mainbundle] loadnibnamed:@"newsarchivetitletvcell" owner:self options:nil]; cell = tvcell; self.tvcell = nil;
normally isn't done subclassing, notice how didn't declare class customcell, vanilla uitableviewcell. how @ pesky subviews can configure them? using tags normal way:
... #define kmykewllabeltag 1 ... uilabel *kewllabel = (uilabel *) [cell viewwithtag:kmykewllabeltag]; kewllabel.text = [nsstring stringwithformat:@"hi there row %d!", indexpath.row]; ...
edit: edit: here's bit more detail, comments short address "what's going on here?" question. here's excerpt 1 of apps loads uitableviewcell .xib:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"myshoppingcarttvcell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { [[nsbundle mainbundle] loadnibnamed:@"shoppingcarttvcell" owner:self options:nil]; cell = tvcell; self.tvcell = nil; } ... // (insert code grab model data row) ... uilabel *namelabel = (uilabel *) [cell viewwithtag:1]; namelabel.text = itemnamestr; uilabel *countlabel = (uilabel *) [cell viewwithtag:2]; countlabel.text = [nsstring stringwithformat:@"%d", itemcount]; uiimageview *iv = (uiimageview *) [cell viewwithtag:3]; ...
here's what's going on here:
there no custom uitableviewcell subclass, there .xib file named "shoppingcarttvcell.xib" containing uitableviewcell, , ui elements placed inside uitableviewcell. ui elements data must change per row assigned unique tag (the tag field in cmd-1 attributes inspector in ib) code can handle objects change them (customize labels, images, etc). make sure don't use "0" since elements default have 0 tag. also, make sure identifier field of uitableviewcell in cmd-1 attributes inspector cellidentifier string.
the file's owner of .xib file table view controller want display cell. more precisely, can class containing iboutlet uitableviewcell *tvcell; member. instance of class pass in owner loadnibnamed:owner:options:. long value of linked outlet nil in owner, when call loadnibnamed:owner:options, outlet of owner filled in object .xib (as long connection made in .xib in ib). understanding magic moment in apple programming opens whole new vistas :).
you must set
self.tvcell = nil;
prepare next cellforrowatindexpath needs load .xib. set nil before loadnibnamed:owner:options:, think that's bit safer actually.
here's how go loading uitableviewcells .xib:
in xcode, add iboutlet uitableviewcell *tvcell; uitableviewcontroller class (including property declaration if like)
in xcode project, create new file, user interface, empty xib. open .xib in ib
in ib, drag tableviewcell library empty .xib file under first responder
click file's owner, cmd-4 (identify inspector), , under class select class containing iboutlet uitableviewcell *tvcell added (probably uitableviewcontroller subclass, class you're manipulating table).
control-drag file's owner uitableviewcell, , select outlet want hook up. field hold newly-loaded-from-xib uitableviewcell when call loadnibnamed:owner:options instance of file's owner "owner" parameter.
add ui elements uitableviewcell (make sure they're inside uitableviewcell hierarchy). elements want customize per-row require unique tag value.
follow recipe gave above in cellforrowatindexpath
have magic moment start understand how .xib files , file's owner objects work together, , start creating lots of cool uitableviewcells , other custom view objects in ib because it's easy , way better creating them in code (imnsho).
Comments
Post a Comment