[英]tableview with different section and row number
由於我是新手,所以我決定與你們一起檢查問題,看看我在做什么錯?
func numberOfSections(in tableView: UITableView) -> Int {
return data.count ?? 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
for eachmain in data! {
header.append(eachmain.unitPlaque!)
}
return header[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let data = data { return Pphone.count }
return 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell") as? SmsTableViewCell
cell?.PhonNumberLbl.text = Pphone[indexPath.row]
cell?.NameLbl.text = Nname[indexPath.row]
return cell!
}
上面的代碼顯示了我填充表格視圖的方式。 但每個部分的行數可能不同。 但是在這里,每個部分的行數都相同!
即使我嘗試了下面的代碼,但它說不能將字符轉換為字符串
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell") as? SmsTableViewCell
cell?.PhonNumberLbl.text = Pphone[indexPath.section][indexPath.row]
cell?.NameLbl.text = Nname[indexPath.section][indexPath.row]
return cell!
}
api響應:
[
{
"contacts" : [
{
"id" : 10155,
"selected" : true,
"name" : "ygfb",
"phoneNumber" : "09123809556"
},
{
"id" : 10159,
"selected" : true,
"name" : "hff",
"phoneNumber" : "08523698522"
},
{
"id" : 9827,
"selected" : true,
"name" : "owner",
"phoneNumber" : "09203137799"
}
],
"unitNo" : 1,
"unitPlaque" : "jack",
"billText" : "textetx"
},
{
"contacts" : [
{
"id" : 10145,
"selected" : true,
"name" : "mmm",
"phoneNumber" : "0912380567"
}
],
"unitNo" : 2,
"unitPlaque" : "mm",
"billText" : "textext"
}
]
模態課:
typealias smsModelList = [SmsModel]
struct SmsModel: Codable {
var unitNo:Int?
var unitPlaque:String?
var billText:String?
var contacts:[ContactsModel?]
}
typealias contactlistmodel = [ContactsModel]
struct ContactsModel: Codable {
var id :Int?
var selected :Bool?
var phoneNumber : String?
var name : String?
}
假設data
為[SmsModel]?
。 以下解決方案將起作用:
func numberOfSections(in tableView: UITableView) -> Int {
return data?.count ?? 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return data?[section].unitPlaque ?? ""
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data?[section].contacts?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell") as! SmsTableViewCell
cell.PhonNumberLbl.text = data?[indexPath.section].contacts?[indexPath.row].name
cell.NameLbl.text = data?[indexPath.section].contacts?[indexPath.row].phoneNumber
return cell
}
您在所有部分中獲得的行數均相同,因為您在
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let data = data
{
return Pphone.count
}
return 0
}
您可以在其中使用if .. else用於其他部分
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
return Pphone.count
} else if section == 2 {
return 0
}
}
您必須獲取行數的contact
計數。 我希望數據是從API獲取的全部內容的數組。 以及存儲在eachmain
中的內容的每個對象。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let dataAtSection = data[section] { // you are using eachmain for this i think , use something like dataAtSection = data[section] as eachmain()
return dataAtSection.contacts.count
}
return 0
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.