【问题标题】:Messages displayed in arrays using swift使用 swift 在数组中显示消息
【发布时间】:2020-05-08 22:34:13
【问题描述】:
现在我在表view中有两个部分,并且选择某个行时,它会移动到另一个部分。有没有办法在没有单元格占用该部分时显示诸如“视图为空”之类的消息,然后当单元格确实占用该部分时,该消息被清除?
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) {
    }
}

【问题讨论】:

  • 您能否详细说明您希望它在选择单元格时完成什么?代码不是很清楚。从我所见,您似乎希望第一部分中的单元格清除第二部分中的匹配行?
  • 代码已经将一个单元格从一个部分移动到另一个部分,但目前在程序开始时,部分 2 是空白的。在这个空白部分,我想显示一条消息。然后,一旦用户从第一个单元格录制了一行,该单元格将移动到第二个部分。一旦发生这种情况,最初在第 2 节中的消息应该会消失。

标签: ios arrays swift xcode


【解决方案1】:
{
   ...
   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
}

【讨论】:

  • 当我尝试使用它时,我收到一条错误消息,提示“未解决使用标识符‘aEmptyCell’。你知道我该如何解决这个问题吗?
  • 你应该像创建目标单元格一样创建它,它是你想要在没有数据时显示的内容。
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多