ios - 如何使用 Swift 在 Tableview Cell 中添加 UITableview

标签 ios swift uitableview

在我的场景中,我尝试在静态 UITableview 单元格中添加动态 UITableview。如何实现这一目标?我尝试了下面的代码,但它对我不起作用。

基于动态表格 View 单元格计数需要重新调整静态表格 View 单元格高度。请提供一些示例代码。

Tableview代码

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

最佳答案

创建一个单一 View 项目,在 Storyboard中添加 TableView 并设置其数据源和委托(delegate)

对firstViewController.swift执行如下代码

import UIKit

  class firstViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

  override func viewDidLoad() {
      super.viewDidLoad()
            }

  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 文件表,并且将以编程方式创建其单元格。

对 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 - 如何使用 Swift 在 Tableview Cell 中添加 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59498607/

相关文章:

ios - 在 CNContactPicker 中选择多个联系人/联系人属性

ios - TableViewController 中的 AdBannerView 显示在 TableView 之后

objective-c - UITableViewCell<MFMailComposeViewControllerDelegate> 崩溃 didReceiveMemoryWarning

ios - 无法查看 UITableViewCell 中的自定义标签

swift - RxSwift : Set initial value for UIPickerView

ios - 如何在 UITableView [Swift] 中设置特定的节标题

iOS 图表未构建

ios - Xcode 无法为 ios 13.3.1 设备构建 flutter 代码,但在 13.3 ios 模拟器上运行代码

ios - xcode7 launchScreen.storyboard无法添加图像

swift - 计时器功能崩溃的应用程序