gpt4 book ai didi

ios - 是否可以在 UITableViewCell 中添加 UITableView

转载 作者:IT王子 更新时间:2023-10-29 07:41:58 24 4
gpt4 key购买 nike

听到的只是我想实现这个的想法,

我想实现像书一样的页面,为此我想将 UITableView 旋转 90 度并将其单元格旋转 90 度,现在我想子类化 UITableViewCell,现在在这个 tableview 单元格中可以添加 UITableview 所以该用户可以垂直滚动以查看内容,用户也可以水平滚动以转到旋转 tableview 的下一个单元格。我只是在想,有没有更好的方法来实现这一点。

最佳答案

是的,我在 UITableView 单元格中添加了 UITableVIew..:)

无需在 xib 文件中添加 tableview 单元格 - 只需将 UITableviewCell 子类化并使用下面的代码,将以编程方式创建一个单元格。

//in your main TableView 

#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 版本创建一个single view projectstoryboard 中添加tableview 并设置它的datasourcedelegate

将下面的代码粘贴到 ViewController.swift

  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
}
}

创建一个新文件 CustomCell.swift,它是 UITableViewCell 的子类并且 不要使用 xib 选择 这个文件没有 .xib 文件 table 及其 cell 将以编程方式创建,就像在 objective-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()
}

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!
}
}

关于ios - 是否可以在 UITableViewCell 中添加 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17398058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com