簡體   English   中英

UITableView章節標題故障?

[英]UITableView Section Title Glitch?

我有一個視圖控制器,里面有一個分組的tableview。 這會產生一個非常奇怪的故障,導致sectionTitles顯示在側面。 我已將topview,trailing,bottom和leading約束附加到tableview的superview上,並且我已經測試了在cellForRowAtIndexPath中返回UITableViewCell()以查看它是否是導致問題的單元格,但它仍然顯示為這樣。

var sectionTitles = ["Customer Orders", "Unprocessed Orders", "Processed Orders"]


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

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

func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
    return sectionTitles
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Perform segue to order list in future
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 117.0
}

在此輸入圖像描述

幾個小時后我發現我沒有實現這個方法

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionTitles[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
    return sectionTitles
}

如果您只有一個部分,則不會放置很多標題。 如果你想為每個單元格提供一個標題,你必須在自定義的tableViewCell和你的cellForRowAtIndexPath添加一個標簽,你必須放置這樣的東西(在你創建了一個NSObject類之后):

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell

let order = Order[indexPath.row]

cell.yourTitleLabel.text = order.title

    return cell
}

暫無
暫無

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

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