簡體   English   中英

數據在UItableview部分重復

[英]Data repeats in section of UItableview

我有一個UITableView有兩個部分和四行。 每個部分應該具有兩行內容。 一切正常,除了在第2節中重復了這些項目:

在此處輸入圖片說明

但是,我想要這樣:

在此處輸入圖片說明

我的代碼如下所示:

迅捷4

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

填充單元格和部分的變量:

// 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 ")
]

這些是表配置:

// 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
    }

這三行:

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

有2個部分,每個部分在表中有2行。 它們映射到4個元素的數組。 所以index.section從0到1, index.row也從0到1,意味着總共4個,與數組的長度匹配。

您需要正確進行翻譯:

let index = indexPath.section * 2 + indexPath.row

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

但是,有一個警告:如果您希望每個部分具有不同的行數,則數據的結構方式將帶來很多麻煩。

1)首先,您必須為剖面數據創建結構

Struct SectionData {
     Let title: String
     Let data: [serverData]

}

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

2)然后我們為每個部分的標題填充數據

    // data filled in my array "section" and in my array "myServerInfo" of type serverData    

let myServerInfo = [SectionData]()
Let section1 = SectionData(title: "section1", data: 
    serverData(structHosts: "www.google.com", structStatusImagesMain: "error", structServerStatusMain: "Error "), 
    serverData(structHosts: "www.amazon.com", structStatusImagesMain: "error", structServerStatusMain: "Error "))

Let section2 = SectionData(title: "section2", data: 
    serverData(structHosts: "www.ebay.com", structStatusImagesMain: "error", structServerStatusMain: "Error "), 
    serverData(structHosts: "www.apple.comt", structStatusImagesMain: "error", structServerStatusMain: "Error "))

myServerInfo.append(section1)
myServerInfo.append(section2)

3)配置表View

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

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myServerInfo[section].data.count
}

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.section][indexPath.row].structHosts
                imgServer.image = UIImage(named: myServerInfo[indexPath.section][indexPath.row].structStatusImagesMain)
                lblServerStatus.text  = myServerInfo[indexPath.section][indexPath.row].structServerStatusMain
            }

    return cell
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM