繁体   English   中英

带有不同部分和行号的表视图

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM