我已设置delegate
& datasource
of tableview in swift和section of section返回正确的值,但numberOfRowsInSection
总是返回4.所以我有两个问题
当节数为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;
}
);
任何帮助都会受到赞赏。
答案 0 :(得分:0)
调整了一些数据结构&amp;在您的代码中输入类型,我尝试了一个示例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
}
}