创建一个数组以存储单元格索引存储表单元按钮值



我当前正在处理一个程序,其中我的tableView在每一行中具有分段控件。我已将标签附加到每个分段控件上,该标签与该行分割控件所对应的行(即分段控件1对应于第1行等(。此外,分段控件具有其自己的索引(选项0对应于0和选项1的索引,对应于1(

的索引。

我想拥有一个数组,该数组将分段的控制索引的值存储在与分段控件标签相对应的位置。即,如果第一行的分段控制索引为1,则第二行的分段控制索引为0,第三行的分段控制索引为1,数组应输出:[1,0,1]

我最好的想法是创建一个数组,将数组的索引指定为分段控件的标签,然后在该索引上指定值为分段控件索引的值(对不起,我知道它的混乱(。

我确定这是某人以前遇到的编程问题,但是我找不到任何信息。我正在Swift进行编程,但是由于这是一个合乎逻辑的问题,而不是一个编程,所以我愿意接受任何其他语言的解决方案。

谢谢尼克

P.S。这是到目前为止该应用程序的图片,我正在使用的代码供您参考。

https://i.stack.imgur.com/ezmuj.png

导入Uikit

类mytableViewController:uitaiteViewController {

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

}

为什么不使用字典代替数组?这样,OU可以存储并轻松从其标签中查找段控件的索引,反之亦然:

// 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]()

使用像数组一样的数组将起作用,只要数组中的项目数与单元格匹配。因此,在您的表视图控制器中,您有一个带有初始值的数组。这样的东西:

var dataArray: [Int] = [1,0,1,1,1,1,0]

然后,您可以使用数组来确定所需的行数,以使它们总是像这样匹配:

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

基本思想是将数据表示与UI分开。

在相关注释中,为了帮助您在创建单元格时遇到问题。您可以使用Dequeuereusablecell从表中获取一个单元格,然后将标签和分段控件添加到其上。当表滚动时,将开始重复使用单元格,您将获得已经添加标签和分段控件的一个单元格,您将在其中添加另一个标签和分段控件。这将继续前进,直到您在每个单元格上都有大量标签和段控件为止。您需要避免这样做,否则最终您的应用可能会崩溃。

最新更新