IOS Swift 4 развернуть и свернуть табличное представление не работает и вызывает сбой - PullRequest
1 голос
/ 13 мая 2019

Ниже приведен мой код для добавления ячейки представления заголовка раздела

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "menuHeaderTableViewCellID") as! MenuHeaderTableViewCell
    cell.foodMenuItems = menuResult?.foodMenuItems?[section]
    cell.setParentUI()
    cell.expandCollapseClicked = {
        [weak self]
        (postiton) in
        let isCollapsed = self?.menuResult?.foodMenuItems?[postiton].isCollapsed ?? false
        self?.menuResult?.foodMenuItems?[postiton].isCollapsed = !isCollapsed
        self?.tableViewMenu?.beginUpdates()
        self?.tableViewMenu?.reloadSections([section], with: .fade)
        self?.tableViewMenu?.endUpdates()

    }
    return cell
}

Ниже приведен код для подсчета в каждой строке и разделе

 func numberOfSections(in tableView: UITableView) -> Int {
    return (menuResult?.foodMenuItems?.count) ?? 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let rowCount = menuResult?.foodMenuItems?[section].items?.count ?? 0
    let isCollpased = menuResult?.foodMenuItems?[section].isCollapsed ?? false
    return isCollpased ? 0 : rowCount
}

Ниже приведен код внутри ячейки представления заголовка

@IBAction func expandCollapseClicked(_ sender: UIButton) {
    guard let superView = self.superview as? UITableView else {
        return
    }
    expandCollapseClicked?(superView.indexPath(for: self)?.section ?? 0)
}

Я сталкиваюсь с проблемой при свертывании первого раздела, мой заголовок исчезает, и когда я пытаюсь свернуть другой раздел, получая следующее исключение, как это исправить?

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (25), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
 *** First throw call stack:

1 Ответ

0 голосов
/ 13 мая 2019

Проблема может быть в:

  1. Избегайте dequeueReusableCell. Попробуйте использовать UIView с меткой и кнопкой.
  2. expandCollapseClicked = {...} должен быть вызван UIButton. поскольку его отправителем является UIButton. Попробуйте использовать:

    view.button.expandCollapseClicked = {...}

Надеюсь, это сработает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...