【问题标题】:How to add Dynamic Tableview at the bottom of static tableview using Swift?如何使用 Swift 在静态表视图底部添加动态表视图?
【发布时间】:2019-12-27 05:29:21
【问题描述】:

在我的场景中,我有一个静态的tableview。现在我需要在静态tableview 的底部添加动态tableview。根据我的情况,我无法将两者混合在一个 tableview 中。所以,我必须添加静态tableview 页脚或任何其他底部附件视图的动态表格视图底部。 我尝试了以下方法

  1. 在静态 cell tableview 中添加了 footer 视图,并且在该页脚中我添加了动态 tableview,但动态 tableview 也不会根据其单元格计数扩展页脚高度,它不允许动态单元格选择。
  2. 添加了accessory视图但不知道怎么添加到tableview的底部

我的 StaticTableview 单元格代码

override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return " "
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return 2
        } else if(section == 1) {
            return 1
        } else if(section == 2) {
            return 3
        } else {
            return 1
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as UITableViewCell
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 0 && indexPath.row == 0 { } 
        else if indexPath.section == 0 && indexPath.row == 1 {
        } else if indexPath.section == 1 && indexPath.row == 0 {
        } else if indexPath.section == 2 && indexPath.row == 0 {
        } else if indexPath.section == 2 && indexPath.row == 1 {
        } else if indexPath.section == 2 && indexPath.row == 2 {
        } else if indexPath.section == 2 && indexPath.row == 3 {
        } else {
        }
    }

【问题讨论】:

  • 是什么阻止您拥有两个表格视图控件?
  • @ElTomato 仅在我维护两个 tableview 时滚动问题。现在我将另一个静态单元格添加到该单元格中的静态表格视图中,我正在尝试添加动态表格视图。
  • 创建 2 个 UITableView 以便您实现目标。没有这个你做不到。
  • @AyazAkbar 静态表格视图单元格中的表格视图怎么样? stackoverflow.com/questions/17398058/…
  • @iosdev 所以你需要在 UITableViewCell 中创建 UItableView。这是正确的方法。

标签: ios swift uitableview


【解决方案1】:

创建一个单视图项目,在 storyboard 中添加 tableview 并设置它的数据源和委托

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

我希望它对你有用...:)

【讨论】:

    【解决方案2】:

    只需在当前tableView 中添加sectionsrows 并在需要时为这些行使用不同的单元格并重新加载。不要让 2 tableView 一个低于另一个。

    var sections = 4
    
    override func numberOfSections(in tableView: UITableView) -> Int {
            return sections
        }
    
    // sections = 5
    // tableView.reloadData()
    
    

    并在运行时更改sections,并相应地处理数据源和委托方法并重新加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      相关资源
      最近更新 更多