gpt4 book ai didi

ios - 在 TableView 的单元格中创建 TableView

转载 作者:行者123 更新时间:2023-11-30 11:23:05 25 4
gpt4 key购买 nike

我有一个包含状态列表的 TableView 。我没有使用 TableViewController 来创建 TableView 。我只是简单地将 TableView 拖到 Storyboard中。我想知道如何在状态 TableView 的每个单元格中生成 TableView 。在每个州单元格内的表格 View 中,我希望它包含另一个表格 View ,其中包含专门适用于该州的城市列表。例如,“密歇根”单元包含城市“底特律”和“弗林特”,而“伊利诺伊”单元包含城市“芝加哥”和“罗克福德”(每个州将包含两个以上的城市,这只是一个示例。 )

下面我附上了我的代码。您能否提供清晰的编码以及放置新代码的位置。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!

let textCellIdentifier = "TextCell"

var states = ["Illinois", "Indiana", "Kentucky", "Michigan", "Ohio", "Pennsylvania", "Wisconsin"]

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self
}

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return states.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath)
let row = indexPath.row
cell.textLabel?.text = states[row]

return cell
}

private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)

let row = indexPath.row
print(states[row])
}
}

最佳答案

也许您需要表格 View 中的部分。只需尝试此代码即可获得结果。

enter image description here

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var mytbl: UITableView!

var names = ["Illinois": ["Chicago", "Rockford"], "Michigan": ["Detroit", "Flint"]]

struct States {
var sectionName : String!
var sectionObjects : [String]!
}
var stateArray = [States]()


override func viewDidLoad() {
super.viewDidLoad()

for (key, value) in names {
print("\(key) -> \(value)")
stateArray.append(States(sectionName: key, sectionObjects: value))
}
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

extension ViewController : UITableViewDelegate,UITableViewDataSource{

func numberOfSections(in tableView: UITableView) -> Int {
return stateArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stateArray[section].sectionObjects.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

// Configure the cell...
cell.textLabel?.text = stateArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}

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

return stateArray[section].sectionName
}

}

关于ios - 在 TableView 的单元格中创建 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51071532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com