I am implementing a tableview(Lets say Main tableview) which has another tableview(call it inner tableview) in its cell. So in viewcontroller i am calling api and appending two nested arrays.
When i am using static data for the tableview its showing correct data but when i am using data from response its showing correct data only to first 3-4 cells after scrolling cells starts showing wrong and repeated data.
I have tried below code.
I have two arrays.
var memberArray = [[String]]()
var memberTestsArray = [[[String]]]()
var listArray: ListResponse?
In view controller i have,
i have function,
func getAllBookings() {
var urlRequest = URLRequest(url: URL(string: "mylistUrl")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
urlRequest.headers = APINetworkClass.shared.getHeader()
urlRequest.httpMethod = "Get"
// print("urlRequest==>>",urlRequest)
URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if let error = error {
print(error)
} else if let data = data ,let responseCode = response as? HTTPURLResponse {
do {
// Parse your response here.
let jsonResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as AnyObject
print("all data new==>>",jsonResponse)
do {
let bookingArray = try JSONDecoder().decode(BookingRevamp.self, from: data)
var results: [[[String]]] = []
self.membersTestsAndPackageArray = results
} catch {
print("inside catch one error==>>",error)
}
}catch{
print("inside catch two error ",error)
}
DispatchQueue.main.async {
self.bookingListTableview.reloadData()
}
}
}.resume()
}
also mainTableview for which i have below code,
func numberOfSections(in tableView: UITableView) -> Int {
return listArray?.results?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let bookingListRevampCell = self.bookingListTableview.dequeueReusableCell(withIdentifier: "bookingListRevampTableViewCell", for: indexPath) as? bookingListRevampTableViewCell
bookingListRevampCell?.sectionArray = self.memberArray[indexPath.section]
bookingListRevampCell?.memberTestPackageArray = self.membersTestsArray[indexPath.section]
return bookingListRevampCell ?? UITableViewCell()
}
And in my tableviewcell,
I have arrays , labels and tableview with header and footer,
var sectionArray = [String]()
var memberTestPackageArray = [[String]]()
@IBOutlet weak var bookingViewMembersTableview: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
bookingViewMembersTableview.delegate = self
bookingViewMembersTableview.dataSource = self
self.bookingViewMembersTableview.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)
}
override func prepareForReuse() {
super.prepareForReuse()
idLabel.text = ""
statusLabel.text = ""
dateTimeLabel.text = ""
amountLabel.text = ""
viewBookingLabel.text = ""
}
and innerTableviewMethods are as below,
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return memberTestPackageArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let bookingViewMemberCell = tableView.dequeueReusableCell(withIdentifier: "innerViewMembersTableViewCell", for: indexPath) as! innerViewMembersTableViewCell
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.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
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footView = UIView()
footView.backgroundColor = .white
let label = UILabel(frame: CGRect(x: 15, y: 0, width: tableView.frame.size.width - 30, height: 30))
label.lineBreakMode = .byWordWrapping
label.textColor = #colorLiteral(red: 0.1369999945, green: 0.3569999933, blue: 0.5569999814, alpha: 1)
label.text = "Download"
footView.addSubview(label)
//add download button instead of label
return footView
}
The tableview is dynamic height tableview.
When i am scrolling updown the data is changing.
How can i fix this?