简体   繁体   English

为什么我只能在TableView Swift中显示我的第一部分?

[英]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.

相关问题 扩展或不扩展时仅重新加载 tableview 的一部分 - Swift - Reload only section of tableview when expanding or not - Swift 是否可以只在tableview中修复第一部分? - is it possible to only first section fixed in tableview? 隐藏在UITableViewController中的TableView的第一节标题-Swift 3-Xcode8 - Hide First Section Header of a TableView Grouped in a UITableViewController - Swift 3 - Xcode8 如何在 swift IOS 中构造我的数据以在 tableView 中使用? - How can I structure my data in swift IOS to use in tableView? 我是否可以根据tableView部分iOS Swift更改collectionView中的单元格数量? - Can i change the number of cells in collectionView based on the tableView Section iOS Swift? Swift-tableView中的可移动行仅在一个区域内,而不在两个区域之间 - Swift - Movable rows in tableView only within a section, not between 为什么我的变量在printLn中改变了但在tableview中却没有改变 - Why is my variable is changing in printLn but not in the tableview Swift 如何在表格视图的第一部分中显示数组的第一项,而在下一部分中显示其余部分? - How to display the first item of my array in the first section and the remaing in the next section in a tableview? 为什么我的 TableView 只显示每个单元格中加载的最后一个图像? (迅速) - Why does my TableView only show the last image loaded in every cell? (swift) iOS:为什么我的表格视图的第一行是空白的? - IOS: why the first row of my tableview was blank?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM