uitableview - Create and use custom prototype table cells in xamarin ios using storyboard -
is possible create , use 'custom' prototype table cells in xamarin ios (monotouch) using storyboard?
i find stuart lodge explaining method using xib/nibs: http://www.youtube.com/watch?feature=player_embedded&v=vd1p2gz8jfy
let's answer question! looking one. :)
1) open storyboard have viewcontroller tableview:
add prototype cell (if there no cell added before):
customize cell want (in case there custom uiimage , label):
remember set height of cell. select whole tableview , properties window select "layout" tab. on top of properties window should see "row height" - put appropriate value:
now select prototype cell once again. in properties window type name of class (it create code-behind class it). in case "friendscustomtableviewcell". after provide "identifier" cell. can see "friendcell". last thing set "style" property set custom. "name" field should empty. once click "enter" after typing "class" code-behind file automatically created:
now code behind cell should below:
public partial class friendscustomtableviewcell : uitableviewcell { public friendscustomtableviewcell (intptr handle) : base (handle) { } public friendscustomtableviewcell(nsstring cellid, string friendname, uiimage friendphoto) : base (uitableviewcellstyle.default, cellid) { friendnamelabel.text = friendname; friendphotoimageview.image = friendphoto; } //this methods update cell data when reuse: public void updatecelldata(string friendname, uiimage friendphoto) { friendnamelabel.text = friendname; friendphotoimageview.image = friendphoto; } }
in uitableviewsource have declare cellidentifier @ top of class (in case "friendcell") , in "getcell" method have cast cells , set data them:
string cellidentifier = "friendcell"; public override uitableviewcell getcell(uitableview tableview, nsindexpath indexpath) { friendscustomtableviewcell cell = (friendscustomtableviewcell) tableview.dequeuereusablecell(cellidentifier); friend friend = _friends[indexpath.row]; //---- if there no cells reuse, create new 1 if (cell == null) { cell = new friendscustomtableviewcell(new nsstring(cellidentifier), friend.friendname, new uiimage(nsdata.fromarray(friend.friendphoto))); } cell.updatecelldata(friend.username, new uiimage(nsdata.fromarray(friend.friendphoto))); return cell; }
that's it, can use custom cells. hope help.
Comments
Post a Comment