簡體   English   中英

iOS:TableView中的多個部分無法正常工作

[英]iOS: Multiple Section in TableView not working

我正在開發一個應用程序,其中需要顯示帶有標題的多行。 就我而言,僅顯示一個部分。 我已經搜索了所有內容,但找不到合適的解決方案。

Here is my code:

class Timeline: UITableViewCell {
      @IBOutlet weak var timelineData: UITextView!
}

class StudenTimelineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let section = ["pizza", "deep dish pizza", "calzone"]

let items = [["Margarita", "BBQ Chicken", "Peproni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns & mashrooms"]]

override func viewDidLoad() {
    super.viewDidLoad()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items[section].count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return section.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.section[section]
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineId", for: indexPath) as! Timeline
    let gpsData = items[indexPath.section][indexPath.row]

    cell.timelineData.text = gpsData
    return cell
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}

What I am getting 在此處輸入圖片說明

我將如何獲得所有部分。 提前致謝。

這是因為您的方法名func numberOfSectionsInTableView(tableView: UITableView) -> Int不正確,因此沒有被調用。

將名稱替換為func numberOfSections(in tableView: UITableView) -> Int然后看魔術發生了。

// MARK:-表格視圖數據源

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewCell", for: indexPath)
    cell.selectionStyle = UITableViewCellSelectionStyle.none

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension

}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension

}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    let cell: UITableViewCell
    cell = tableView.dequeueReusableCell(withIdentifier: "tableviewHeader")!

    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.backgroundColor = UIColor.white

    return cell
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 32;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM