У меня есть UITableView с двумя разделами и четырьмя строками. Каждый раздел должен иметь две строки содержимого.
Все работает нормально, за исключением того, что пункты повторяются в разделе 2:
Однако я хочу, чтобы это было так:
Мой код выглядит следующим образом:
swift 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
}