是否可以在UITableViewCell中添加UITableView [英] Is it possible to add UITableView within a UITableViewCell
问题描述
听到的只是我想要实现这个想法,
我想实现书喜欢的页面,为此我想采取UITableView和旋转90度和它的单元格90度,现在我想要子类化UITableViewCell,现在在这个tableview单元格内,可以添加UITableview,以便用户可以垂直滚动看到的内容,用户也可以水平滚动去转到下一个单元格的旋转tableview。
是的,这是可能的,我在UITableView单元格中添加了UITableVIew
..:)
不需要在xib文件中添加tableview单元格,只需要子类化UITableviewCell并使用下面的代码,
/ / in your main Table view
#importViewController.h
#importCustomCell.h
@interface ViewController()< UITableViewDataSource,UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//在加载视图之后执行任何其他设置,通常来自nib。
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}
- (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 :@细胞];
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;
}
//在您的自定义tableview单元格
// .m文件
#importCustomCell.h
@implementation CustomCell
@synthesize dataAraay; //数组保存子菜单数据
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier: reuseIdentifier];
if(self){
//初始化代码
self.frame = CGRectMake(0,0,300,50);
UITableView * subMenuTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; // create tableview a
subMenuTableView.tag = 100;
subMenuTableView.delegate = self;
subMenuTableView.dataSource = self;
[self addSubview:subMenuTableView]; // add it cell
[subMenuTableView release]; // for without ARC
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated]
//为所选状态配置视图
}
- (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); //设置tableview的框架
}
//管理数据源和委托子菜单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版本
创建查看项目
在 storyboard
中添加 tableview
并设置其在
ViewController.swift $>中的
和委托
c $ c>下面的代码
import UIKit
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
override func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从一个nib进行任何额外的设置。
}
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}
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
}
}
文件 CustomCell.swift
这是 UITableViewCell
和的子类不要使用xib
此文件没有
.xib
文件表
及其 code>将以
目标c代码
中的编程方式创建。
在 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()
}
b $ b required init(coder aDecoder:NSCoder){
fatalError(init(coder :) has not been implemented)
setUpTable()
}
override func awakeFromNib(){
super.awakeFromNib()
//初始化代码
setUpTable()
}
func setUpTable()
{
subMenuTable = UITableView(frame:CGRectZero,style:UITableViewStyle.Plain)
subMenuTable?.delegate = self
subMenuTable?.dataSource = self
self.addSubview(subMenuTable! )
}
覆盖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 animated:animated)
//为所选状态配置视图
}
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)
如果(cell == nil){
cell = UITableViewCell(style:UITableViewCellStyle.Default,reuseIdentifier:cellID)
}
cell?.textLabel?.text = dataArr [indexPath.row]
return cell!
}
}
Hear is just the idea what i am thinking to implement this,
I want to implement book like pages, for this i want to take UITableView and rotated-90 degree and its cell by 90 degree, and now i want to subclass UITableViewCell, now within this tableview cell it is possible to add UITableview so that user can scroll vertically to see the contents and user can also scroll horizontally to go to next cell of rotated tableview. It is just i am thinking, is there any better way to implement this.
yes it is possible, i added the UITableVIew within the UITableView cell .. :)
no need to add tableview cell in xib file just subclass the UITableviewCell and use the code below, cell will be created programatically.
//in your main Table view
#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()<UITableViewDataSource , UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be 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 your custom tableview cell
// .m file
#import "CustomCell.h"
@implementation CustomCell
@synthesize dataAraay; //array to 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 a
subMenuTableView.tag = 100;
subMenuTableView.delegate = self;
subMenuTableView.dataSource = self;
[self addSubview:subMenuTableView]; // add it cell
[subMenuTableView release]; // for without ARC
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the 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 the frames for tableview
}
//manage datasource and delegate for 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 a single view project
add tableview
inside storyboard
and set up its datasource
and delegate
in ViewController.swift
past below code
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be 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 a new file CustomCell.swift
which is the subclass of UITableViewCell
and do not select with xib
this file is without .xib
file table
and its cell
will be created programatically as in objective-c code
.
in CustomCell.swift
past below code
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 the view for the 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!
}
}
这篇关于是否可以在UITableViewCell中添加UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!