SegmentControl复制UITableviewCell上的复选标记

时间:2018-05-02 13:00:12

标签: ios swift uisegmentedcontrol

基本上我正在使用pod M13Checkbox,当我没有使用我的UISegmentedControl时它非常有用。我有两个待办事项列表分为两个部分。只要我检查第一段上的一个方框,它就会在第二段重复。

我不确定这是否是因为细胞被重复使用。使用print语句我已经发现我能够看到indexpath.row的数据而忽略了当前显示的段。只是我无法控制的复选标记。

请忽略任何“Firebase备注”。

class ToDoList: UITableViewController {
    @IBOutlet weak var segment: UISegmentedControl!

    var selectedRow: Int = 0

    let seg1 = ["first","tab","Index"]
    let seg2 = ["second","segment","akjdhfajd"]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var returnValue = 0

        switch(segment.selectedSegmentIndex) {
        case 0: returnValue = seg1.count
        case 1: returnValue = seg2.count
        default: break
        }

        return returnValue
    }

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

        switch (segment.selectedSegmentIndex) {
        case 0:
            segment.selectedSegmentIndex = 0
            selectedRow = 0
            cell.item.text = seg1[indexPath.row]
            cell.box.tag = indexPath.row
        case 1:
            segment.selectedSegmentIndex = 1
            selectedRow = 1
            cell.item.text = seg2[indexPath.row]
            cell.box.tag = indexPath.row
            print(cell.box.tag)
        default:
            break
        }

        cell.box.addTarget(self, action: Selector(("checkboxPressed")), for: UIControlEvents.touchUpInside)
        return cell
    }

    @IBAction func checkboxPressed(_ sender: M13Checkbox) {
        let buttonRow = sender.tag
        let listItem = [buttonRow]
        let todoDict = ["\(buttonRow)": "True"]
        //need to individually checkbox the cases .. at moment if i check one it checks the other also

        if sender.checkState == .checked || sender.checkedValue == nil {
            print("\(buttonRow)")

            if selectedRow == 0 {
                let listArrayItem = seg1[buttonRow]
                sender.checkedValue = listArrayItem

                print(listArrayItem)
            } else {
                print("we are on segment 2")
            }
        } else if sender.checkState == .unchecked {
            // if its unchecked. then u want to remove it from the checked item in firebase.. we will only load checked items obviosuly..
            //remove from completed list
        }
    }
}

0 个答案:

没有答案