I am implementing a nested UITableview that is having a UITableview which has another UITableview within the cell, both of UITableviews are have auto resize cell with dynamic data and header&footer as well. So the problem here i am facing is that when the data is large in innerTableview, the application getting crash, some times when i scroll upto two/three cells it works but then it crash. I have nested arrays through which i am assigning data to the tableview.
Below is approach that i am working with.
I have Two arrays and a UITableview within main/parent view controller.
@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"]]
]
Then my UITableview delegate and datasource methods are as below.
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
}
And inside the RevampTableViewCell, I have two arrays and a UITableview as below,
@IBOutlet weak var bookingViewMembersTableview: UITableView!
var sectionArray = [String]()
var memberTestPackageArray = [[String]]()
Then my Inner UITableview delegate and datasource methods are as below.
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
}
The above code that works well as excepted but the problem(crash) occurs when the data for the inner tableview is large(like multiple rows inside section).
Or in more simple way if I replace the sectionRowArray,
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"]]
]
with the below array,
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"]]
]
How can i solve this crash? I have been stuck in this problem for 3 days but couldn’t find any working solution.
The above data is dummy data but the real data that i am fetching is also similar to it.