[英]why can I only present my first section in TableView Swift ?
I have created two arrays called section one and two to feed in their data to my table. 我创建了两个数组,分别称为第一节和第二节,以将它们的数据输入到表中。
let Sec1 = ["Section" , "One"]
let Sec2 = ["Section", "Two"]
And I have create an array called sectionTitles
which contains the name of my sections. 我创建了一个名为sectionTitles
的数组,其中包含我的节的名称。
let sectionTitles = ["Section1" , "Sectoin2"]
And then I have the following delegate table view functions : 然后,我具有以下委托表视图功能:
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return sectionTitles[section]
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
switch section {
case 0 :
return Sec1.count
default:
return Sec2.count
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = challengeTable.dequeueReusableCell(withIdentifier: cellID) as! CustomisedCell
cell.selectionStyle = .none
switch indexPath.section {
case 0:
cell.CAChallengeTitle.text = Sec1[indexPath.row]
cell.CAChallengeDescription.text = "\(Sec1[indexPath.row]) "
cell.CAChallengeButt.tag = indexPath[1]
cell.CAChallengeButt.setTitle("I am interested >", for: .normal)
print("Done with sectoin 0")
default:
cell.CAChallengeTitle.text = Sec2[indexPath.row]
cell.CAChallengeDescription.text = "\(Sec2[indexPath.row]) "
cell.CAChallengeButt.tag = indexPath[1]
cell.CAChallengeButt.setTitle("I am interested >", for: .normal)
print("Done with sectoin 1")
}
return cell
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
let height : CGFloat = 60
return height
}
But in the end I only get to present the fist section only. 但是最后我只介绍了第一部分。 Any idea where am I making my mistake ? 知道我在哪里犯错吗?
You haven't implemented the numberOfSections
data source method. 您尚未实现numberOfSections
数据源方法。
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitles.count
}
Without this, the table assumes 1 section. 否则,该表将假定为1部分。
You have to implement numberOfRowsInSection method of tableView in case you are using only static sactions then you have to return the count of sections in numberOfRowsInSection method. 如果仅使用静态动作,则必须实现tableView的numberOfRowsInSection方法,然后必须在numberOfRowsInSection方法中返回节数。 otherwise, store all the sections in an array and return Array.count
否则,将所有节存储在数组中并返回Array.count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.