numberOfRowsInSection始终以swift格式返回固定值
我已经在swift中设置了tableview的numberOfRowsInSection始终以swift格式返回固定值,swift,tableview,Swift,Tableview,我已经在swift中设置了tableview的委托和数据源,节数返回正确的值,但节数始终返回4。所以我有两个问题 我不知道为什么它总是返回4 当节数为5时,为什么要多次调用大于5的numberOfRowsInSection func numberOfSectionsInTableView(tableView: UITableView) -> Int { println("No of Sections: \(self.contactArr.count)") if self.
委托
和数据源
,节数返回正确的值,但节数始终返回4。所以我有两个问题
numberOfRowsInSection
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;
});
}
联系人=({
接触=({
日期=“2015-09-07 05:00:30”;
message=“亲爱的sund博士,一切进展顺利,我还不能给您一个预期的完成时间。”;
name=sund;
前缀=”;
“用户信息id”=3;
});
},{
联系人=({
日期=“2015-09-07 05:38:23”;
message=“亲爱的Krishan Kumar Sharma医生,我们在康复室,一切顺利。”;
name=“Krishan Kumar Sharma”;
前缀=“博士”;
“用户信息id”=4;
},{
日期=“2015-09-07 07:51:57”;
message=“亲爱的Krishan Kuram Sharma先生,在康复室,一切都很顺利,很快就会有人与您交谈。”;
name=“Krishan Kumar Sharma”;
前缀=“博士”;
“用户信息id”=4;
});
},{
联系人=({
日期=“2015-09-22 02:37:21”;
message=“亲爱的xyz先生,一切进展顺利,我还不能给您一个预期的完成时间。”;
name=xyz;
前缀=“先生”;
“用户信息id”=19;
});
},{
联系人=({
日期=“2015-09-2809:28:26”;
message=“亲爱的测试人员先生,一切进展顺利,我还不能给您一个预期的完成时间。”;
名称=测试仪;
前缀=“先生”;
“用户信息id”=20;
},{
日期=“2015-09-2809:31:39”;
message=“亲爱的测试人员先生,一切进展顺利,我们预计在下一个小时内完成。”;
名称=测试仪;
前缀=“先生”;
“用户信息id”=20;
},{
日期=“2015-09-2809:32:01”;
message=“亲爱的测试人员先生,我们在恢复室,一切顺利。”;
名称=测试仪;
前缀=“先生”;
“用户信息id”=20;
});
},{
联系人=({
日期=“2015-09-29 03:08:33”;
message=“亲爱的杰克逊先生,一切进展顺利,我还不能给您一个预期的完成时间。”;
姓名=杰克逊;
前缀=”;
“用户信息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
}
}
导入UIKit
类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:[字典]=[]
重写func viewDidLoad(){
super.viewDidLoad()
contactArr=[contactDict1、contactDict2、contactDict3、contactDict4、contactDict5]
//加载视图后,通常从nib执行任何其他设置。
tableView.frame=self.view.frame
tableView.delegate=self
tableView.dataSource=self
tableView.registerClass(UITableViewCell.self,强制重用标识符:“单元格”)
self.view.addSubview(tableView)
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
打印(“节数:\(self.contactArr.count)”)
如果self.contactArr.count>0{
返回self.contactArr.count
}
否则{
返回0
}
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
打印(“当前节\(节)”)
让contactDic=self.contactArr[部分]作为字典
打印(“contactDic=\(contactDic)”)
设lcontactArr=contactDic[“contact”]!作为数组
返回lcontactArr.count;
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cell:UITableViewCell=tableView.dequeueReusableCellWithIdentifier(“cell”)!
让contactDic=self.contactArr[indexPath.section]作为字典
设lcontactArr=contactDic[“contact”]!作为数组
cell.textlab?.text=lcontactArr[indexPath.row]作为字符串
返回单元
}
}
在您的代码中调整了一些数据结构和类型转换之后,我尝试了一个示例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
}
}
导入UIKit
类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:[字典]=[]
重写func viewDidLoad(){
super.viewDidLoad()
contactArr=[contactDict1、contactDict2、contactDict3、contactDict4、contactDict5]
//加载视图后,通常从nib执行任何其他设置。
tableView.frame=self.view.frame
tableView.delegate=self
tableView.dataSource=self
tableView.registerClass(UITableViewCell.self,强制重用标识符:“单元格”)
self.view.addSubview(tableView)
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
印刷品