简体   繁体   中英

tableview with different section and row number

As I'm new to swift I decided to check the problem with you guys to see what am I doing wrong?

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!
}

The code above shows the way I'm populating the table view. but each section might have a different number of rows. but here I get the same number of rows for each section!

even I tried the code below but it says that character cannot be converted to a string

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 Response :

    [ 
    {
    "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"
  }
]

Modal class:

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?
}

Supposing data as [SmsModel]? . Below solution will work:

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
}

You are getting the same number of rows in all sections because you are using same data in

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if let data  = data
    {
        return Pphone.count
    }
return 0
}

where you can use if.. else for different section

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   if section == 1 {
    return Pphone.count 
  } else if section == 2 {
  return 0
}
}

You have to get the count of contact for the number of rows. I am hoping data is the array of your whole content getting from the API. And the each object of the content you are storing in 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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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