我目前正在开发一个程序,其中每行都有一个带有分段控件的 TableView。我已将一个标记附加到每个分段控件,该标记对应于分段控件所在的行(即分段控件 1 对应于行 1,等等)。另外,分段控件有自己的索引(选项0对应索引0,选项1对应索引1)
我想要一个数组,将分段控件索引的值存储在与分段控件的标记相对应的位置。即,如果第一行的分段控制索引为 1,第二行的分段控制索引为 0,第三行的分段控制索引为 1,则数组应输出: [1,0,1]
我最好的想法是创建一个数组,将数组的索引指定为分段控件的标记,然后将该索引处的值指定为分段控件的索引值(抱歉,我知道它很困惑)。
我确信这是一个以前有人遇到过的编程问题,但我找不到任何相关信息。我正在使用 swift 进行编程,但由于这更像是一个逻辑问题,而不是编程问题,所以我愿意接受任何其他语言的解决方案。
谢谢, 尼克
附注 这是迄今为止该应用程序的图片以及我正在使用的代码供您引用。
导入UIKit
MyTableViewController 类:UITableViewController {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Sample Item"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let actionButton = YSSegmentedControl(
frame: CGRect.zero,
titles: [
"Yes",
"No"
])
actionButton.delegate = self
cell.addSubview(nameLabel)
cell.addSubview(actionButton)
actionButton.translatesAutoresizingMaskIntoConstraints = false
cell.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]-160-[v1]-16-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel, "v1": actionButton]));
cell.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
cell.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-60-[v0]-60-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": actionButton]))
actionButton.tag = indexPath.row
cell.backgroundColor = .green
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
// 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 MyTableViewController: YSSegmentedControlDelegate {
func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int) {
print("\(segmentedControl.tag)" + "\(index)")
}
func segmentedControl(_ segmentedControl: YSSegmentedControl, didPressItemAt index: Int) {
}
}
最佳答案
为什么不使用字典而不是数组呢?这样您就可以存储并轻松地从其标记中查找段控件的索引,反之亦然:
// Storing index and corresponding tag
var indexToTag = [Int:Int]()
indexToTag[index] = tag
// Looking up tag
let tag = indexToTag[index]
您也可以执行相反的操作:
// Storing tag and corresponding index
var tagToIndex = [Int:Int]()
tagToIndex[tag] = index
// Looking up tag
let index = tagToIndex[tag]
我不确定您到底想要实现什么,但您甚至可以存储 UISegmentedControls
的数组,以便您可以通过 indexRow.path
引用它们并获取无论您想要什么信息:
var mySegments = [UISegmentedControls]()
您甚至可以在字典中使用它们
var tagToSegment = [Int:UISegmentedControls]()
关于ios - 创建一个数组以在单元格索引处存储表格单元格按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395161/