簡體   English   中英

單元格無法在表格視圖中正確顯示

[英]Cell doesn't show up properly in table view

我正在使用TableView在ViewController上實現它。 我面臨的問題是細胞數量。 當前,單元格應該返回2行,但僅顯示一行。

我想實現的目標

在此處輸入圖片說明

故事板-您可以忽略除tableview之外的其余組件

在此處輸入圖片說明

import UIKit


class ViewController: UIViewController {

    @IBOutlet var locationTableView: UITableView!

    let locationArray = ["Current Location", "Where to"]
    let picArray = ["currentPic.png", "whereTo.png"]

    override func viewDidLoad() {
        super.viewDidLoad()


        locationTableView.delegate = self
      }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return locationArray.count
    }


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

     let location = locationArray[indexPath.row]
     let pic = picArray[indexPath.row]
        print(location)
        if let locationCell = cell as? GetLocationTableViewCell {
            locationCell.locationTitle.text = location
            locationCell.iconImage.image = pic
        }

     return cell

     }
}

GetLocationTableViewCell.swift

    import UIKit

    class GetLocationTableViewCell: UITableViewCell {


        @IBOutlet var iconImage: UIImageView!

        @IBOutlet var locationTitle: UILabel!

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

    }

結果真的很奇怪

在模擬器上

在此處輸入圖片說明

在我的iPhone上

在此處輸入圖片說明

在兩個平台上,行數不同。 我做錯了什么? 是因為限制嗎?

這是問題locationArray[indexPath.section] , picArray[indexPath.section]您的部分返回0,因此您需要使用indexPath.row兩次顯示“當前位置”

嘗試這個

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

     let location = locationArray[indexPath.row]
     let pic = picArray[indexPath.row]
        print(location)
        if let locationCell = cell as? GetLocationTableViewCell {
            locationCell.locationTitle.text = location
            locationCell.iconImage.image = pic
        }

     return cell

     }

像這樣更改表委托方法:

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

     let location = locationArray[indexPath.section]
     let pic = picArray[indexPath.row]//replace this line
        print(location)
        if let locationCell = cell as? GetLocationTableViewCell {
            locationCell.locationTitle.text = location
            locationCell.iconImage.image = pic
        }

     return cell

     }

您可以像貝婁一樣嘗試。 你可以這樣寫數組

 let locationArray = [
    ["textName":"Current Location", "imageName":"currentPic"],
    ["textName":"Where to", "imageName":"whereTo"]
]

表視圖協議方法看起來像

     override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
       return 1
  }

     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      return locationArray.count
   }


  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) CustomCell

    cell.textLabel?.text = locationArray[indexPath.row]["textName"]
    cell.imageView?.image = UIImage(named: locationArray[indexPath.row]["imageName"]!)

      return cell
     }

替換方法cellRowAtPath中的cellRowAtPath

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

     let location = locationArray[indexPath.row]
     let pic = picArray[indexPath.row]
        print(location)
        if let locationCell = cell as? GetLocationTableViewCell {
            locationCell.locationTitle.text = location
            locationCell.iconImage.image = pic
        }

     return cell

}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 102 //Give cell height
}

根據您的問題,我建議您管理要應用於tableview和Cell的約束。 在這里,我提供了要遵循的說明以獲取解決方案。 請設法對您的項目遵循相同的指示,希望這可以解決您的問題。

1.管理表視圖約束

一種。 上,左,右將表視圖固定為超級視圖

設置tableview的高度限制。

在此處輸入圖片說明

C。 為表格視圖設置高度限制的出口,以便我們可以在運行時更改值。 在此處輸入圖片說明

2.管理單元約束

一種。 將您的單元格內容從頂部,左側,右側,底部固定超級視圖 在此處輸入圖片說明 3.在運行時管理tableview約束

一種。 將表格視圖視圖的高度限制設置為表格單元在索引路徑處拖曳的表格視圖的內容大小。

self.tableHeight.constant = self.locationTableView.contentSize.height

完整的控制器代碼。

//  Controller.swift
//  Copyright © 2017 dip. All rights reserved.
//

import UIKit


class ViewController1: UIViewController {

    //1:
    //Outlet of tableview Height constraints
    @IBOutlet weak var tableHeight: NSLayoutConstraint!

    @IBOutlet var locationTableView: UITableView!

    let locationArray = ["Current Location", "Where to"]
    let picArray = ["currentPic.png", "whereTo.png"]

    override func viewDidLoad() {
        super.viewDidLoad()

        locationTableView.delegate = self
    }
}

extension ViewController1: UITableViewDelegate, UITableViewDataSource {


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return locationArray.count
    }


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

        let location = locationArray[indexPath.row]
        let pic = picArray[indexPath.row]
        print(location)
        if let locationCell = cell as? GetLocationTableViewCell {
            locationCell.locationTitle.text = location
            locationCell.iconImage.image = pic
        }

        //2. set tableivew view height to content Height
        self.tableHeight.constant = self.locationTableView.contentSize.height


        return cell

    }
}

放出

在此處輸入圖片說明

暫無
暫無

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

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