[英]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 : 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"
}
]
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]?
假设data
为[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 您可以在其中使用if .. else用于其他部分
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. 您必须获取行数的contact
计数。 I am hoping data is the array of your whole content getting from the API. 我希望数据是从API获取的全部内容的数组。 And the each object of the content you are storing in eachmain
. 以及存储在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.