我正在尝试制作一个带有嵌套UITableViews
的可扩展菜单,其中单击一个类别将展开以显示该类别中的项目(此部分现在可以使用),其中每个项目都是自定义UITableViewCell
,其中在商品名称旁边有一个标签,在标签下方还有一个UITableView
(tvInner)。单击该项目时,我想将菜单数据更新到该自定义单元格(此部分现在可以使用),然后我希望该自定义单元格将另一个单元格(cell_itemrow)添加到该内部UITableView
(tvInner)并显示它(这是行不通的部分)。
VC_Main (我的ViewController类具有外部UITableView
( tvOuter ):
import UIKit
struct Collapsed{
var category_opened = Bool()
var category_id = Int()
var category_name = String()
var items = [Item]()
}
struct Item{
var name = String()
var rows = [Row]()
}
struct Row{
var size: Int
}
struct FlatItem {
var itemid: Int
var category_id: Int
var category_name: String
var name: String
}
class VC_Main: UIViewController {
@IBOutlet weak var tvOuter: UITableView!
var collapsedMenu = [Collapsed]()
var flatMenu = [
FlatItem(itemid: 0, category_id: 0, category_name: "category1", name: "item1"),
FlatItem(itemid: 2, category_id: 0, category_name: "category1", name: "item2"),
FlatItem(itemid: 3, category_id: 0, category_name: "category1", name: "item3"),
FlatItem(itemid: 4, category_id: 1, category_name: "category2", name: "item4"),
FlatItem(itemid: 7, category_id: 1, category_name: "category2", name: "item5"),
FlatItem(itemid: 9, category_id: 1, category_name: "category2", name: "item6")]
override func viewDidLoad() {
super.viewDidLoad()
let mapping = Dictionary(grouping: flatMenu, by: { $0.category_id })
collapsedMenu = mapping.map {
Collapsed(
category_opened: false,
category_id: $0.key,
category_name: $0.value.first?.category_name ?? "",
items: $0.value.map { Item(name: $0.name, rows: []) }
)
}
.sorted { $0.category_id < $1.category_id }
tvOuter.delegate = self
tvOuter.dataSource = self
tvOuter.tableFooterView = UIView(frame: CGRect.zero)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension VC_Main: UITableViewDataSource, UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return collapsedMenu.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if collapsedMenu[section].category_opened == true{
return collapsedMenu[section].items.count + 1
}else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{ // category cell type
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell_category") else {return UITableViewCell()}
cell.textLabel?.text = collapsedMenu[indexPath.section].category_name
return cell
}else{ // item cell type
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_item") as! cell_item
cell.itemdata = collapsedMenu[indexPath.section].items[indexPath.row - 1]
cell.lblItemName.text = collapsedMenu[indexPath.section].items[indexPath.row - 1].name
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0{ // we clicked a category heading (cell_category)
if collapsedMenu[indexPath.section].category_opened == true{
collapsedMenu[indexPath.section].category_opened = false
}else{
collapsedMenu[indexPath.section].category_opened = true
}
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}else{ // we clicked an item (cell_item)
collapsedMenu[indexPath.section].items[indexPath.row - 1].rows.append(Row(size: 0))
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
}
我的自定义UITableViewCell
类 cell_item (其中包含内部UITableView
( tvInner )以容纳更多内部UITableViewCells
( strong> cell_itemrow ):
import UIKit
class cell_item: UITableViewCell, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var ivItemImage: UIImageView!
@IBOutlet weak var lblItemName: UILabel!
@IBOutlet weak var tvInner: UITableView!
var itemdata = Item()
override func awakeFromNib() {
super.awakeFromNib()
setUpTable()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpTable()
}
override func layoutSubviews() {
super.layoutSubviews()
//tvInner?.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 setUpTable(){
tvInner.delegate = self
tvInner.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemdata.rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_itemrow") as! cell_itemrow
cell.lblRowLabel.text = itemdata.name
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none) // play around with this animation
return cell
}
}
到目前为止我已确认的内容:
但是随后该单元格未添加或未显示
我要实现的目标:
我已经在Main.storyboard中设置了UITableViews
和UITableViewCells
。
或者,如果有一种无需使用内部UITableView
即可实现的方法,我想知道如何做到这一点。触摸item_cell时,我想添加一个带有删除按钮和标签的新行,单击某行的删除按钮应将其从数据结构中删除,并将其行从视图中删除