数据在 UItableview 的部分重复
Data repeats in section of UItableview
我有一个包含两部分和四行的 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 配置:
// 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
table 中有 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)配置tableView
// 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
}
我有一个包含两部分和四行的 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 配置:
// 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
table 中有 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)配置tableView
// 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
}