Search code examples
arraysswiftuitableviewsections

Data repeats in section of UItableview


I have a UITableView with two sections and four rows. Each section is supposed to have two rows content. It all works fine except for the fact that the items are repeated in section 2:

enter image description here

However, I want to have it like this:

enter image description here

My code looks as follows:

swift 4

// structure for serverData type
struct serverData {
    var structHosts: String
    var structStatusImagesMain: String
    var structServerStatusMain: String
}

The variables to fill cells and sections:

// data filled in my array "section" and in my array "myServerInfo" of type serverData    
let sections = ["Section 1", "Section 2"]
let myServerInfo = [
    serverData(structHosts: "www.google.com", structStatusImagesMain: "error", structServerStatusMain: "Error "), 
    serverData(structHosts: "www.amazon.com", structStatusImagesMain: "error", structServerStatusMain: "Error "), 
    serverData(structHosts: "www.ebay.com", structStatusImagesMain: "error", structServerStatusMain: "Error "), 
    serverData(structHosts: "www.apple.comt", structStatusImagesMain: "error", structServerStatusMain: "Error ")
]

These are the table configurations:

// table functions    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

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

        let lblDescribtion : UILabel = cell.contentView.viewWithTag(6) as! UILabel
        let lblServerStatus : UILabel = cell.contentView.viewWithTag(8) as! UILabel
        let imgServer : UIImageView = cell.contentView.viewWithTag(7) as! UIImageView

                if myServerInfo .isEmpty {
                    print("myServerInfo is empty: ", myServerInfo)
                } else {
                    lblDescribtion.text = myServerInfo[indexPath.row].structHosts
                    imgServer.image = UIImage(named: myServerInfo[indexPath.row].structStatusImagesMain)
                    lblServerStatus.text  = myServerInfo[indexPath.row].structServerStatusMain
                }

        return cell
    }

Solution

  • These three lines:

    lblDescribtion.text = myServerInfo[indexPath.row].structHosts
    imgServer.image = UIImage(named: myServerInfo[indexPath.row].structStatusImagesMain)
    lblServerStatus.text  = myServerInfo[indexPath.row].structServerStatusMain
    

    There are 2 sections, each with 2 rows in your table. They map to an array of 4 elements. So index.section goes from 0 to 1, index.row also goes from 0 to 1, meaning 4 total, matching the length of the array.

    You need to do that translation correctly:

    let index = indexPath.section * 2 + indexPath.row
    
    lblDescribtion.text = myServerInfo[index].structHosts
    imgServer.image = UIImage(named: myServerInfo[index].structStatusImagesMain)
    lblServerStatus.text  = myServerInfo[index].structServerStatusMain
    

    One warning though: the way you structure the data means a lot of troubles if you want each section to have differing number of rows.