gpt4 book ai didi

swift - numberOfRowsInSection 总是快速返回固定值

转载 作者:行者123 更新时间:2023-11-30 14:02:23 26 4
gpt4 key购买 nike

我已经在 swift 中设置了 tableview 的 delegatedatasource ,节数返回正确的值,但 numberOfRowsInSection 始终返回 4。所以我有两个问题

  1. 我不知道为什么它总是返回 4。
  2. 当节数为 5 时,为什么 numberOfRowsInSection 被多次调用且大于 5。

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    println("No of Sections: \(self.contactArr.count)")

    if self.contactArr.count > 0{
    return self.contactArr.count
    }
    else{
    return 0
    }
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    println("Current Section \(section)")
    var contactDic : NSDictionary = self.contactArr.objectAtIndex(section) as! NSDictionary
    var lcontactArr : NSMutableArray = contactDic["contact"] as! NSMutableArray

    return lcontactArr.count;
    }

输出

No of Sections: 5
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4
Current Section 4

我的联系人数组是:

Contacts =({
contact =({
date = "2015-09-07 05:00:30";
message = "Dear Dr. sund, Everything is going well, I can't give you an expected finish time yet.";
name = sund;
prefix = "<null>";
"user_messsage_id" = 3;
});
},{
contact = ({
date = "2015-09-07 05:38:23";
message = "Dear Dr. Krishan Kumar Sharma, We are in the Recovery Room, everything went well.";
name = "Krishan Kumar Sharma";
prefix = "Dr.";
"user_messsage_id" = 4;
},{
date = "2015-09-07 07:51:57";
message = "Dear Mr. Krishan Kuram Sharma, In the Recovery Room, everything went well, someone will speak with you soon.";
name = "Krishan Kumar Sharma";
prefix = "Dr.";
"user_messsage_id" = 4;
});
},{
contact = ({
date = "2015-09-22 02:37:21";
message = "Dear Mr. xyz, Everything is going well, I can't give you an expected finish time yet.";
name = xyz;
prefix = "Mr.";
"user_messsage_id" = 19;
});
},{
contact = ({
date = "2015-09-28 09:28:26";
message = "Dear Mr. tester, Everything is going well, I can't give you an expected finish time yet.";
name = tester;
prefix = "Mr.";
"user_messsage_id" = 20;
},{
date = "2015-09-28 09:31:39";
message = "Dear Mr. tester, Everything is going well, we expect to finish within the next hour.";
name = tester;
prefix = "Mr.";
"user_messsage_id" = 20;
},{
date = "2015-09-28 09:32:01";
message = "Dear Mr. tester, We are in the Recovery Room, everything went well.";
name = tester;
prefix = "Mr.";
"user_messsage_id" = 20;
});
},{
contact = ({
date = "2015-09-29 03:08:33";
message = "Dear Mr. Jackson, Everything is going well, I can't give you an expected finish time yet.";
name = Jackson;
prefix = "<null>";
"user_messsage_id" = 24;
});
}

如有任何帮助,我们将不胜感激。

最佳答案

在调整代码中的一些数据结构和类型转换后,我尝试了一个示例 POC,它对我来说工作得很好。您可以尝试使用此代码一次吗?

import UIKit

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {

var tableView: UITableView = UITableView()

var contactDict1 : Dictionary = ["contact" : ["C1", "C2", "C3", "C4"]]
var contactDict2 : Dictionary = ["contact" : ["C11", "C21", "C31", "C41"]]
var contactDict3 : Dictionary = ["contact" : ["C12", "C22", "C32", "C42"]]
var contactDict4 : Dictionary = ["contact" : ["C13", "C23", "C33", "C43"]]
var contactDict5 : Dictionary = ["contact" : ["C14", "C24", "C34", "C44"]]

var contactArr : [Dictionary<String, Array<String>>] = []

override func viewDidLoad() {
super.viewDidLoad()

contactArr = [contactDict1, contactDict2, contactDict3, contactDict4, contactDict5]

// Do any additional setup after loading the view, typically from a nib.

tableView.frame = self.view.frame
tableView.delegate = self
tableView.dataSource = self

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

self.view.addSubview(tableView)
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
print("No of Sections: \(self.contactArr.count)")

if self.contactArr.count > 0 {
return self.contactArr.count
}
else{
return 0
}
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Current Section \(section)")

let contactDic = self.contactArr[section] as Dictionary

print("contactDic = \(contactDic)")

let lcontactArr = contactDic["contact"]! as Array<String>

return lcontactArr.count;
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")!

let contactDic = self.contactArr[indexPath.section] as Dictionary
let lcontactArr = contactDic["contact"]! as Array<String>

cell.textLabel?.text = lcontactArr[indexPath.row] as String

return cell
}
}

关于swift - numberOfRowsInSection 总是快速返回固定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32839723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com