Reputation: 177
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?
}
Upvotes: 0
Views: 3004
Reputation: 5669
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
}
Upvotes: 2
Reputation: 1849
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
}
Upvotes: 1
Reputation: 101
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
}
}
Upvotes: 1