Ниже описан подход, с которым я работаю.
У меня есть два массива и UITableview в основном/родительском представлении. контроллер.
Код: Выделить всё
@IBOutlet weak var bookingListTableview: UITableView!
var sectionArray = [["Member-One","Member-Two"],["Member-One","Member-Two","Member-Three"],["Member-One"],["Member-One","Member-Two","Member-Three","Member-four"],["Member-One","Member-Two"],["Member-One","Member-Two"]]
var sectionRowArray = [
[["M1-Row-One","M1-Row-Two"],["M2-Row-One","M2-Row-Two"]],
[["M1-Row-One","M1-Row-Two","M1-Row-Three"],["M2-Row-One","M2-Row-Two","M2-Row-Three"],["M2-Row-One","M2-Row-Two"]],
[["M1-Row-One","M1-Row-One"]],
[["M1-Row-One","M1-Row-One"],["M2-Row-One","M2-Row-Two"],["M3-Row-Three","M3-Row-four"],["M4-Row-Three","M4-Row-four"]],
[["M1-Row-One","M1-Row-Two"],["M1-Row-one","M2-Row-Two","M3-Row-Three"]],
[["Row-One","Row-Two",],["Row-One","Row-Two"]]
]
Код: Выделить всё
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray?.results?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let revampCell = self.bookingListTableview.dequeueReusableCell(withIdentifier: "revampTableViewCell", for: indexPath) as! RevampTableViewCell
revampCell.sectionArray = self.sectionArray[indexPath.section]
revampCell.memberTestPackageArray = self.sectionRowArray[indexPath.section]
return revampCell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Код: Выделить всё
@IBOutlet weak var bookingViewMembersTableview: UITableView!
var sectionArray = [String]()
var memberTestPackageArray = [[String]]()
Код: Выделить всё
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return memberTestPackageArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let bookingViewMemberCell = tableView.dequeueReusableCell(withIdentifier: "BookingViewMembersTableViewCell", for: indexPath) as! BookingViewMembersTableViewCell
bookingViewMemberCell.testPackageNameLabel.text = memberTestPackageArray[indexPath.section][indexPath.row]
return bookingViewMemberCell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = .white
let label = UILabel(frame: CGRect(x: 15, y: -15, width: tableView.frame.size.width - 30, height: 30))
label.font = R.font.poppinsSemiBold(size: 15)
label.numberOfLines = 0
label.textColor = #colorLiteral(red: 0.1369999945, green: 0.3569999933, blue: 0.5569999814, alpha: 1)
label.textColor = UIColor.hexString("#000000")
label.text = sectionArray[section]
headerView.addSubview(label)
return headerView
}
Или в других случаях простой способ, если я заменю секциюRowArray,
Код: Выделить всё
var sectionRowArray = [
[["M1-Row-One","M1-Row-Two"],["M2-Row-One","M2-Row-Two"]],
[["M1-Row-One","M1-Row-Two","M1-Row-Three"],["M2-Row-One","M2-Row-Two","M2-Row-Three"],["M2-Row-One","M2-Row-Two"]],
[["M1-Row-One","M1-Row-One"]],
[["M1-Row-One","M1-Row-One"],["M2-Row-One","M2-Row-Two"],["M3-Row-Three","M3-Row-four"],["M4-Row-Three","M4-Row-four"]],
[["M1-Row-One","M1-Row-Two"],["M1-Row-one","M2-Row-Two","M3-Row-Three"]],
[["Row-One","Row-Two",],["Row-One","Row-Two"]]
]
Код: Выделить всё
var sectionRowArray = [
[["M1-Row-One","M1-Row-Two"],["M2-Row-One","M2-Row-Two"]],
[["M1-Row-One","M1-Row-Two","M1-Row-Three"],["M2-Row-One","M2-Row-Two","M2-Row-Three"],["M2-Row-One","M2-Row-Two"]],
[["M1-Row-One","M1-Row-One"]],
[["M1-Row-One","M1-Row-One"],["M2-Row-One","M2-Row-Two"],["M3-Row-Three","M3-Row-four"],["M4-Row-Three","M4-Row-four"]],
[["M1-Row-One","M1-Row-Two","M1-Row-Three","M1-Row-Four","M1-Row-Five","M1-Row-Six","M1-Row-Seven","M1-Row-Eight","M1-Row-Nine","M1-Row-Ten"],["M1-Row-one","M2-Row-Two","M3-Row-Three","M1-Row-Four","M2-Row-Five","M1-Row-Six","M2-Row-Seven"]],
[["Row-One","Row-Two",],["Row-One","Row-Two"]]
]
Приведенные выше данные являются фиктивными, но реальные данные, которые я получаю, также похожи на них.
Подробнее здесь: https://stackoverflow.com/questions/786 ... -scrolling