简体   繁体   中英

Duplicated values in grouped section UITableView swift IOS

I have created grouped sections in UITableView but values are getting duplicate. How to populate items under each section? Sections I already created. Few Title items are null.

SectionList --> Title --> Items

Like:

  • Bir have one item
  • Proj Plan have null item
  • Proj Ev has three items

  • I want to display textField in every section Title.

code:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.dataSource = self
        tableView.delegate = self

        if let url = Bundle.main.url(forResource: "AppD", withExtension: "json") {
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                let jsonData = try decoder.decode(AppointmentDetail.self, from: data)
                self.AppData = jsonData
                self.tableView.reloadData()
            } catch {
                print("error:\(error)")
            }
        }
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return  50
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return AppData?.sectionList?[section].title
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return AppData?.sectionList?.count ?? 0
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return AppData?.sectionList?.count ?? 0
    }

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

        // Configure the cell...


        if let sections = AppData?.sectionList?[indexPath.section].items {
            for item in sections {
                if item.textField != "" {
                    cell.textLabel?.text = item.textField

                }
            }
        }

Make changes as below

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return AppData?.sectionList?[section].items?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
    cell.textLabel?.text = AppData?.sectionList?[indexPath.section].items?[indexPath.row].textField
    cell.textLabel?.sizeToFit()
    cell.textLabel?.numberOfLines = 0
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

For Adding HeaderView XIB to table view

var tableHeaderViewObj : BookNowHotelDetailHeader?

inViewdidload

tableHeaderViewObj = BookNowHotelDetailHeader(frame: CGRect(x: 0.0, y: 0.0, width: (window?.frame.width)!, height: 350))
self.BookNowTV.tableHeaderView = tableHeaderViewObj
tableHeaderViewObj?.parentVC = self
tableHeaderViewObj?.UpdateBookNowHotelData(Obj: hotelDetailObj ?? HotelDetailModal())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM