自定义UITableViewCells不保持顺序或正确的复选标记

时间:2019-03-16 19:52:21

标签: ios swift uitableview

我的自定义uitableviewcell无法按正确的顺序重新加载,并且复选标记与正确的项目不匹配,这很麻烦。

这是我的出发点: enter image description here

作为示例,我将其打乱为这样: enter image description here

关闭并重新启动后,这是我得到的: enter image description here

这是功能的相关部分:

func loadData() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let request = ListItem.createFetchRequest()
    request.returnsObjectsAsFaults = false

    var priority = 0

    if let results = try? context.fetch(request) {
        for result in results {
            priority = Int(result.priorityLevel)!

            items[priority - 1].insert(result, at: 0)
        }
    }
// This is to show me what gets loaded
    var z = 0
    var x = 0
    for i in items {
        x = 0
        for _ in items[z] {
            print(i[x].isChecked, i[x].priorityLevel, i[x].itemText)
            x += 1
        }
        z += 1
    }
// This is to show me what gets loaded
}

func setChecked(cell: ListItemCell) {
    guard let indexPath = self.listTableView.indexPath(for: cell) else {
        // Note, this shouldn't happen - how did the user tap on a button that wasn't on screen?
        return
    }
    let cell = listTableView.cellForRow(at: indexPath) as! ListItemCell

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let request = ListItem.createFetchRequest()
    request.returnsObjectsAsFaults = false

    var tempItems: [[ListItem]] = [[], [], [], [], []]
    var priorityInt = 0

    if let results = try? context.fetch(request) {
        for result in results {
            priorityInt = Int(result.priorityLevel)!
            tempItems[priorityInt - 1].insert(result, at: 0)
        }
    }

    if cell.checkButton.isSelected {
        cell.checkButton.isSelected = false
        tempItems[indexPath.section][indexPath.row].isChecked = false
    }
    else {
        cell.checkButton.isSelected = true
        tempItems[indexPath.section][indexPath.row].isChecked = true
    }

    do {
        try context.save()
    }
    catch let err {
        print(err)
    }
}

func popoverData(priority: String, itemText: String) {
    let cell = listTableView.cellForRow(at: popoverCellIndex) as! ListItemCell
    cell.priorityLabel.text = priority
    cell.itemLabel.text = itemText

    let newIndexPath = IndexPath(row: 0, section: Int(priority)! - 1)

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let request = ListItem.createFetchRequest()
    request.returnsObjectsAsFaults = false

    var tempItems: [[ListItem]] = [[], [], [], [], []]
    var priorityInt = 0

    if let results = try? context.fetch(request) {
        for result in results {
            priorityInt = Int(result.priorityLevel)!
            tempItems[priorityInt - 1].insert(result, at: 0)
        }
    }

    tempItems[popoverCellIndex.section][popoverCellIndex.row].priorityLevel = priority
    tempItems[popoverCellIndex.section][popoverCellIndex.row].itemText = itemText
    tempItems[popoverCellIndex.section][popoverCellIndex.row].isChecked = cell.checkButton.isSelected

    do {
        try context.save()
    }
    catch let err {
        print(err)
    }
    cell.isSelected = false
    items = [[], [], [], [], []]
    loadData()
    listTableView.moveRow(at: popoverCellIndex, to: newIndexPath)
}

显而易见的期望结果是复选标记保留在其相关的单元格中,而这些单元格保持在其设置顺序中。我看不出这是怎么回事。

根据要求,这里是cellForRowAt。我还包括didSelectRowAt。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ListItemCell
    cell.checkButton.isSelected = items[indexPath.section][indexPath.row].isChecked
    cell.priorityLabel.text = items[indexPath.section][indexPath.row].priorityLevel
    cell.itemLabel.text = items[indexPath.section][indexPath.row].itemText
    cell.delegate = self

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = listTableView.cellForRow(at: indexPath) as! ListItemCell
    popoverCellIndex = indexPath
    let controller = PopoverViewController()
    controller.delegate = self
    controller.modalPresentationStyle = .popover
    controller.preferredContentSize = CGSize(width: 300, height: 400)
    controller.priorityPassed = cell.priorityLabel.text!
    controller.textBox.text = cell.itemLabel.text!
    controller.priorityPassed = cell.priorityLabel.text!
    let presentationController = AlwaysPresentAsPopover.configurePresentation(forController: controller)
    presentationController.sourceView = cell
    presentationController.sourceRect = cell.bounds
    presentationController.permittedArrowDirections = [.up, .down]
    presentationController.backgroundColor = UIColor(r: 211, g: 221, b: 230)
    self.present(controller, animated: true)
}

0 个答案:

没有答案