【问题标题】:Can you make a TableView in a custom cell?您可以在自定义单元格中制作 TableView 吗?
【发布时间】:2020-01-14 03:12:35
【问题描述】:

我有一个自定义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。 这可能吗?

谢谢!

【问题讨论】:

  • 你不能写tableView.delegate = self; tableView.dataSource = self,因为你在这个类中的tableView被设置为一个普通的UIView。但是是的,您可以在 UITableViewCell 中添加 UITableView。
  • 是的,这是我的问题,谢谢!

标签: ios swift uitableview custom-cell uitableviewrowaction


【解决方案1】:

你完全可以在 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 或情节提要加载单元格,则使用 initWithCoder

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

请注意,您正在为一条崎岖不平的道路做好准备。完全有可能,但祝你好运!

【讨论】:

  • 更新:我最终没有使用你的代码,我意识到我的故事板有问题,但我能够使用自定义单元格制作一个表格视图,并且每个自定义单元格都有一个表格视图里面有另一个自定义单元哈哈
  • 太棒了,是的,享受你正在下降的兔子洞。 ;)
  • 哈哈谢谢,如果你愿意帮我走下这个兔子洞,我意识到我还有一个问题:stackoverflow.com/questions/59743623/…
  • 我完全理解你是否想避开兔子洞,哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2018-09-28
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多