1 step
// Инициализация необходимых массивов
var titleCoc2 : [String] = []
var titleCoc3 : [String] = []
var titleCoc4 : [String] = []
var titleCoc5 : [String] = []
var titleCoc6 : [String] = []
// Это глобальный массив в котором будут хранится все остальные массивы...
var itemsInfoGlobal : [
Array<
Any>] = []
2 step
let GroupSection = ["---","Описание","Ингредиенты","Как приготовить", "Пищевая ценность"]
3 step
// Заполняем массивы данными
override
func viewDidLoad() {
super.
viewDidLoad()
titleCoc2 = ["1"]
titleCoc3 = ["1", "2",]
titleCoc4 = ["1", "2", "2"]
titleCoc5 = ["1", "2", "2", "2"]
titleCoc6 = ["1", "2", "2", "2", "2"]
// Добавляем все массивы в один - для заполнения секций
itemsInfoGlobal = [titleCoc2, titleCoc3, titleCoc4, titleCoc5, titleCoc6]
}
4 step
override func numberOfSections(in tableView: UITableView) -> Int {
// Количество секций
return GroupSection.
count
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
// Количество строк в каждой секции
return itemsInfoGlobal[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)
}