簡體   English   中英

如何在Swift中以編程方式創建帶有節的Grouped TableView

[英]How to create a Grouped TableView with sections programmatically in Swift

我想知道如何以編程方式創建分組/分段tableview。 我試圖創建的設計是這樣的:

在此輸入圖像描述

我希望頂部有一個UIImageView ,高度為160,接着是3個部分,每個部分都有不同的行數。 然后,我將在每行中放置靜態標簽,然后使用我的數據更新其標簽。

我對這個VC的代碼如下:

import UIKit

class SelectedFilmTableViewController: UITableViewController {

var film: Film? {
    didSet {
        navigationItem.title = film?.Film_Name
    }
}

var cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = film?.Directed_By
    cell.detailTextLabel?.text = film?.Film_Poster_Image_URL

    return cell

}

}

我已經嘗試添加方法來聲明numberOfSections等,但我在構建應用程序時似乎沒有看到它們。 我所看到的只是一個看似正常的桌面視圖。 我錯過了一些設置嗎?

因為我以編程方式導航到此VC,所以我需要在不使用故事板的情況下進行設置。

任何關於如何創建我需要的外觀的幫助表示贊賞,謝謝!

將其他視圖與TableView組合時,我更喜歡制作UIViewController並使用UITableViewController作為嵌入式控制器。

在github上提交了一個例子來查看它

以下是您的表控制器代碼應如下所示:

class MyTableController : UITableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if(section == 2) {
            return "Film Information"
        } else {
            return " "
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(section == 0) {
            return 1
        } else if(section == 1) {
            return 2
        } else {
            return 4
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("SomeCell", forIndexPath: indexPath) as UITableViewCell


        return cell

    }
}

首先,您應該使用以下方法返回3節

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    return 3
}

然后根據傳遞給此方法的節號返回每個節的1,2和4(行):

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // return if section == 0 return 1 and so on...
}

最后使用indexPath傳遞給此方法,根據indexPath中存儲的section和row返回相應的內容:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell

  // if indexPath.section == 0 {
  //  cell.textLabel?.text = film?.Directed_By
  //  cell.detailTextLabel?.text = film?.Film_Poster_Image_URL
  // } else if ....


    return cell

}

你忘了調用reloadData viewDidLoad方法更新為

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)
    tableView.reloadData()
}

如果您需要3個具有不同行數的部分,請將方法更改為

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return [1,2,4][section]
}

要將圖像添加到頂部,請使用UITableView的屬性tableHeaderView

暫無
暫無

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

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