Создание табличного представления с ячейками, разделенными датами - PullRequest
0 голосов
/ 14 мая 2019

Я играю с TableViews и создаю списки. Однако одна вещь, которую я полностью потерял в знании, как это сделать, - создать список задач, разделенных датами.

Например, если бы мне нужно было позвонить другу 15 июня, чтобы поздравить его с днем ​​рождения; вместо таблицы. Скажите мне позвонить ему, она будет разделена этой датой.

Примерно так: https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwjq7YHgvpniAhWRu54KHX7-BMoQjRx6BAgBEAU&url=https%3A%2F%2Fappadvice.com%2Fapp%2Ftower-radio-app%2F1405371224&psig=AOvVaw1Q57CGOQFZ6xm9SG9zHgqK&ust=1557870701032787

Спасибо

1 Ответ

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

Вы должны сделать даты в разделе, и задачи уже находятся в ячейках.примерно так:

import UIKit

struct ToDo {
   let date: Double
   let todo: [String]
}

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var array: [ToDo] = [ToDo(date: 1560640981, todo: ["call a friend to wish him happy birthday"]), ToDo(date: 1557962581, todo: ["Buy mom flowers", "By milk"])]

    override func viewDidLoad() {
        super.viewDidLoad()
      }
}

extension ViewController: UITableViewDataSource {

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

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

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let dateInUnix = array[section].date
        let date = Date(timeIntervalSince1970: dateInUnix)

        let dayTimePeriodFormatter = DateFormatter()
        dayTimePeriodFormatter.dateFormat = "MMM dd YYYY hh:mm a"
        let dateString = dayTimePeriodFormatter.string(from: date)

        return dateString
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = self.array[indexPath.section].todo[indexPath.row]
        return cell
    }
}

И вы получите что-то вроде этого: введите описание изображения здесь

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