sguruapp@gmail.com
facebook vk instagram Телеграм youtube

Создаём секции в TableView программно (Create section for TableView) - Swift 5


Индивидуальное, дистанционное обучение программирование подробнее

// Создаём массив с секциями
let GroupSection = ["---","Описание","Ингредиенты","Как приготовить", "Пищевая ценность"]

// Создаём массив с данными
let itemsInfoArrays = [
["1111111111111111"],
["1.4","1.5","1.6"],
["22", "33"],
["6","7", "8"],
["26","27", "28"]
]


override func numberOfSections(in tableView: UITableView) -> Int {
// Количество секций
return GroupSection.count.
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Количество данных в каждой секции
return itemsInfoArrays[section].count

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

// Заполняем данные в каждую секцию
cell.textLabel?.text = itemsInfoArrays[indexPath.section][indexPath.row]
return cell
}


// Не ЗАБУДЬТЕ ДОБАВИТЬ ЭТОТ МЕТОД!
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let section = self.GroupSection[section]
return section
}

// Метод позволяет работать с секцией
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// Изменяем фон секции
view.tintColor = .clear

// Изменяем цвет текста для секции
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = .gray


// Можно ещё так, если пожелаете
let header = view as! UITableViewHeaderFooterView
header.backgroundView?.backgroundColor = .white
header.textLabel?.textColor = .black
header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
}

Возврат к списку

обучись профессии будущего

Хочешь стать успешным разработчиком
приложений? Тогда добро пожаловать к нам.

обучись профессии будущего