代码之家  ›  专栏  ›  技术社区  ›  Jake2Finn

在uiTableView的节中重复数据

  •  0
  • Jake2Finn  · 技术社区  · 6 年前

    我有一个包含两个部分和四行的UITableView。每个部分应该有两行内容。 除第2节中重复的项目外,一切都很好:

    但是,我想这样做:

    我的代码如下:

    Swift 4

    serverData类型的结构 结构服务器数据{ var structhosts:字符串 var structStatusImagesMain:字符串 var structServerStatusMin:字符串 } < /代码>

    填充单元格和节的变量:

    //data filled in my array“section”and in my array“myserverinfo”of type serverdata
    设区段=[“区段1”,“区段2”]
    让我的服务器信息=[
    serverdata(structhosts:“www.google.com”,structstatusmagesmain:“错误”,structserverstatusmain:“错误”),
    serverdata(structhosts:“www.amazon.com”,structstatusmagesmain:“错误”,structserverstatusmain:“错误”),
    serverdata(structhosts:“www.ebay.com”,structstatusmagesmain:“错误”,structserverstatusmain:“错误”),
    服务器数据(structhosts:“www.apple.comt”,structstatusmagesmain:“错误”,structserverstatusmain:“错误”)。
    ]
    < /代码> 
    
    

    这些是表配置:

    //表函数
    func tableview(u tableview:uitableview,titleforheaderinsection部分:int)->字符串?{
    返回段[段]
    }
    
    func numberofsections(在TableView:uiTableView中)->int{
    返回sections.count
    }
    
    func tableview(u tableview:uitableview,numberofrowspeagon部分:int)->int{
    返回2
    }
    
    func tableview(u tableview:uitableview,cellforrowat indexpath:indexpath)->uitableviewcell{
    让cell=serverstatusable.dequeuerusablecell(带标识符:“serverstatuscell”,用于:indexpath)
    
    让lblDescription:uiLabel=cell.contentView.viewWithTag(6)为!乌拉贝尔
    让lblserverstatus:uilabel=cell.contentview.viewwithtag(8)as!乌拉贝尔
    将imgserver:uiImageView=cell.contentView.viewWithTag(7)设为!UIIVIEVIEW
    
    如果我的服务器信息是空的{
    打印(“myserverinfo为空:”,myserverinfo)
    }否则{
    lblDescribtion.text=myServerInfo[indexPath.row].structhosts
    imgserver.image=uiimage(名为:myserverinfo[indexpath.row].structstatusImagesMain)
    lblserverstatus.text=myServerInfo[indexPath.row].structServerStatusMain
    }
    
    返回单元
    }
    < /代码> 
    
    

    enter image description here

    但是,我想这样做:

    enter image description here

    我的代码如下:

    斯威夫特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
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Code Different    6 年前

    这三行:

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

    有两个部分,每个部分在表中有两行。它们映射到一个由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
    

    不过,有一个警告:如果希望每个部分的行数不同,那么构造数据的方式就意味着很多问题。

        2
  •  1
  •   Bola Ibrahim    6 年前

    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)配制台视图

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