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
}