ios - Is it possible to add UITableView within a UITableViewCell -
hear idea thinking implement this,
i want implement book pages, want take uitableview , rotated-90 degree , cell 90 degree, , want subclass uitableviewcell, within tableview cell possible add uitableview user can scroll vertically see contents , user can scroll horizontally go next cell of rotated tableview. thinking, there better way implement this.
yes possible, added uitableview within uitableview cell .. :)
no need add tableview cell in xib file - subclass uitableviewcell , use code below, cell created programatically.
//in main tableview #import "viewcontroller.h" #import "customcell.h" @interface viewcontroller ()<uitableviewdatasource , uitableviewdelegate> @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (void)dealloc { [_atv release]; [super dealloc]; } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 3; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { customcell *cell = [self.atv dequeuereusablecellwithidentifier:@"cell"]; if(cell == nil) { cell = [[[customcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cell"]autorelease]; } cell.dataaraay = [nsmutablearray arraywithobjects:@"submenu->1",@"submenu->2",@"submenu->3",@"submenu->4",@"submenu->5", nil]; return cell; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 150; } //in custom tableview cell // .m file #import "customcell.h" @implementation customcell @synthesize dataaraay; //array hold submenu data - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // initialization code self.frame = cgrectmake(0, 0, 300, 50); uitableview *submenutableview = [[uitableview alloc]initwithframe:cgrectzero style:uitableviewstyleplain]; //create tableview submenutableview.tag = 100; submenutableview.delegate = self; submenutableview.datasource = self; [self addsubview:submenutableview]; // add cell [submenutableview release]; // without arc } return self; } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } -(void)layoutsubviews { [super layoutsubviews]; uitableview *submenutableview =(uitableview *) [self viewwithtag:100]; submenutableview.frame = cgrectmake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5);//set frames tableview } //manage datasource , delegate submenu tableview -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return dataaraay.count; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cellid"]; if(cell == nil) { cell = [[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cellid"]autorelease]; } cell.textlabel.text = [self.dataaraay objectatindex:indexpath.row]; return cell; } @end
swift version create single view project
add tableview
inside storyboard
, set datasource
, delegate
paste code below viewcontroller.swift
import uikit class viewcontroller: uiviewcontroller,uitableviewdatasource,uitableviewdelegate { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func numberofsectionsintableview(tableview: uitableview) -> int { return 3; } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return 1; } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell:customcell? = tableview.dequeuereusablecellwithidentifier("cell") as? customcell if cell == nil { cell = customcell(style: uitableviewcellstyle.default, reuseidentifier: "cell") } cell?.dataarr = ["submenu->1","submenu->2","submenu->3","submenu->4","submenu->5"] return cell! } func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { return 150.0 } }
create new file customcell.swift
subclass of uitableviewcell
, do not select xib
file without .xib
file table
, cell
created programatically in objective-c code
.
paste code below customcell.swift
import uikit class customcell: uitableviewcell,uitableviewdatasource,uitableviewdelegate { var dataarr:[string] = [] var submenutable:uitableview? override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style , reuseidentifier: reuseidentifier) setuptable() } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") setuptable() } override func awakefromnib() { super.awakefromnib() // initialization code setuptable() } func setuptable(){ submenutable = uitableview(frame: cgrectzero, style:uitableviewstyle.plain) submenutable?.delegate = self submenutable?.datasource = self self.addsubview(submenutable!) } override func layoutsubviews() { super.layoutsubviews() submenutable?.frame = cgrectmake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5) } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return dataarr.count } func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell: uitableviewcell? = tableview.dequeuereusablecellwithidentifier("cellid") if cell == nil { cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "cellid") } cell?.textlabel?.text = dataarr[indexpath.row] return cell! } }
Comments
Post a Comment