我正在制作一个允许用户选择项目的 tableview,primary 是主要项目,每个主要项目都包含一个辅助列表,我创建了一个自定义单元格并在里面插入了另一个 tableview,如下图链接所示。
Form picture
这是我的 View Controller 。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PrimaryDelegate {
@IBOutlet weak var tableView: UITableView!
//This is my data
var cardList = Card(userName: "Kevin", primaryList: [Card.Primary(primaryName: "Card1", secondaryList: [Card.Primary.Secondary(secondaryName: "Card1-1")]),Card.Primary(primaryName: "Card2", secondaryList: [Card.Primary.Secondary(secondaryName: "Card2-1"),Card.Primary.Secondary(secondaryName: "Card2-2")])])
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
@IBAction func enterAction(_ sender: Any) {
//I hope here can print the result
//How should I get the result from primaryList and secondaryList in Custom Cell ?
print(cardList)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cardList.primaryList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.primaryLabel.text = cardList.primaryList[indexPath.row].primaryName
cell.secondaryList = cardList.primaryList[indexPath.row].secondaryList
cell.primaryIndex = indexPath.row
cell.primaryDelegate = self
return cell
}
func primaryIndex(index:Int) {
//I use delegate to get index, but how to tell which secondaryList needs to be selected all?
print("primaryIndex\(index)")
}
}
这是我的自定义单元格,它包括另一个表格 View ,这就是我感到复杂的原因。
class CustomTableViewCell: UITableViewCell,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var primaryBtn: UIButton!
@IBOutlet weak var secondaryBtn: UIButton!
@IBOutlet weak var primaryLabel: UILabel!
@IBOutlet weak var secondaryLabel: UILabel!
@IBOutlet weak var secondaryTableView: UITableView!
@IBOutlet weak var secondaryHeight: NSLayoutConstraint!
var primaryIndex:Int?
var primaryDelegate:PrimaryDelegate?
var secondaryList:[Card.Primary.Secondary]!{
didSet{
secondaryTableView.delegate = self
secondaryTableView.dataSource = self
secondaryTableView.reloadData()
secondaryHeight.constant = secondaryTableView.contentSize.height
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return secondaryList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.secondaryLabel.text = secondaryList[indexPath.row].secondaryName
return cell
}
@IBAction func primaryBtnAction(_ sender: UIButton) {
sender.isSelected = !primaryBtn.isSelected
primaryDelegate?.primaryIndex(index: primaryIndex!)
}
@IBAction func secondaryBtnAction(_ sender: UIButton) {
sender.isSelected = !secondaryBtn.isSelected
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我希望它可以...
1.当用户选择主要项目时,它可以自动帮我选择所有次要项目。但是,主项只能选择“一个”,当用户选择下一个主项时,需要取消前一项,包括所有次项。
2.当用户按下enterAction时,它可以打印用户选择的数据。我需要知道如果用户不选择主要的,有多少次要列表的项目被选中。
我的意思是结果是 Card1-1 和 Card2-1,他们只选择二级列表中的项目。
当我选择主项时,我应该如何告诉自定义单元格的表格 View 全选,以及自定义单元格如何知道选择了哪个主项并需要重新加载数据?
如果需要更多信息,请告诉我,这个选择规则让我很困惑。谢谢
最佳答案
您可以在 primaryBtnAction
上使用它:
if sender.isSelected{
for section in 0..<tableView.numberOfSections {
for row in 0..<tableView.numberOfRows(inSection: section) {
tableView.selectRow(at: IndexPath(row: row, section: section), animated: false, scrollPosition: .none)
}
}
} else { //Deselect statement
tableView.deselectRow(at: IndexPath(row: row, section: section), animated: false)
}
希望能帮助到你...
关于ios - 如何使用一个tableview的按钮自动选择另一个tableview的按钮全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61541077/