Сообщения, отображаемые в массивах с использованием swift - PullRequest
0 голосов
/ 09 мая 2020

Прямо сейчас у меня есть два раздела в табличном представлении, и когда выбрана определенная строка, она перемещается в другой раздел. Есть ли способ отобразить сообщение, такое как «представление пусто», когда ни одна ячейка не занимает раздел, а затем, когда ячейка действительно занимает раздел, сообщение очищается?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var goalTableView: UITableView!

    let sections: [String] = ["Mark as Complete:", "History:"]
    var goals: [[String]] = [["goal 1", "goal 2", "goal 3"], []]

    override func viewDidLoad() {
        super.viewDidLoad()

        let headerView = UIView()
        goalTableView.tableHeaderView = headerView
        headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 5)
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return goals[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodayGoalViewCell_1", for: indexPath) as? GoalTableViewCell
        cell?.goalLabel.text = goals[indexPath.section][indexPath.row]
        cell?.cellDelegate = self
        cell?.index = indexPath
        return cell!
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return goals.count
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        progressBarAnimation()
        if indexPath.section == 0 {
            goals[1].append(goals[0][indexPath.row])
            goals[0].remove(at: indexPath.row)
            tableView.reloadData()
        }
    }

}

extension ViewController: GoalTableView {
    func selectGoalButton(index: Int) {
    }
}

1 Ответ

1 голос
/ 09 мая 2020
{
   ...
   goals[1].append(goals[0][indexPath.row])
   if goals[1].first == theEmptyModel {
       goals[1].removeFirst()
   }
   goals[0].remove(at: indexPath.row)
   if goals[0].count == 0 {
      goals[0].append(theEmptyModel)
   }
   ...

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   ...
   var cell: UITableViewCell

   let model = goals[indexPath.section][indexPath.row]
   if model == theEmptyModel {
      cell = aEmpetyCell
   } else {
      cell = aGoalTableViewCell
   }
   ...
   return cell
}
...