如何使用 Swift 在静态表视图的底部添加动态表视图?



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

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

我的静态表格视图单元格代码

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

创建单个视图项目 在情节提要中添加表视图并设置其数据源和委托

对第一个视图控制器进行如下编码.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 文件表,其单元格将以编程方式创建。

对自定义单元进行如下编码.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!
}
}

我希望它能为你工作:)

只需在当前tableView中添加sectionsrows,并在需要时为这些行使用不同的单元格并重新加载。 不要将 2 个tableView一个接一个地做。

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

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

最新更新