ios - 你能在自定义单元格中制作 TableView 吗?

标签 ios swift uitableview custom-cell uitableviewrowaction

我有一个自定义的 TableViewCell。在这里面,我想放另一个 TableView,但我遇到了问题。 我有这样的东西:

import UIKit

class ByteCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var game: UIImageView!
    @IBOutlet weak var TableView: UIView!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

}

首先,我不会写:

tableView.delegate = self
tableView.dataSource = self

这里的任何地方,所以我根本无法修改 tableView。

其次,确实出现的TableView,没有任何行,也不能滚动。我基本上想在 TableView 内的自定义单元格内创建一个可滚动的 TableView。 这可能吗?

谢谢!

最佳答案

你绝对可以在 UITableViewCells 中放置一个 UITableView

将 .delegate = self 放在哪里取决于您如何创建单元格。

如果您以编程方式创建它,则使用 override init

 override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
    }

但是,如果您从 nib 或 Storyboard加载单元格,则使用 initWithCoder

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
       tableView = UITableView()
       tableView.delegate = self
       tableView.dataSource = self
    }
    return self;
}

请注意,您正在为一条崎岖不平的道路做好准备。尽管如此,还是有可能的,祝你好运!

关于ios - 你能在自定义单元格中制作 TableView 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59727037/

相关文章:

ios - 如何阅读本地化的 plist 作为字典?

ios - 如何根据其中的组件设置 UITableViewCell 高度

iphone - 如何在 AQClient 线程中追踪 iPad 应用程序的奇怪崩溃?

ios - 从 View 推送到 View Controller 无法快速工作

objective-c - 如何在 iOS 中旋转时移动一个 UIView?

ios - 如果 Swift 按值而不是引用传递,为什么我可以通过将它传递给函数来在 Swift 中操作 UIView 的属性?

ios - 架构 armv7 的 undefined symbol : "_OBJCCLASS$_ALAssetsLibrary"

ios - 在 Objective-C 项目中找不到 Swift 类

ios - 重新排序 TableView 和节标题高度变为零时数据源不会更新

ios - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array