【问题标题】:Change label text when height expand of cell of tableView in swiftswift中tableView的单元格高度扩展时更改标签文本
【发布时间】:2018-01-24 06:08:21
【问题描述】:

我是IOS新手,我正在使用swift。我想在单元格高度扩展时更改标签文本。请告诉我如何在swift中单元格高度扩展时更改文本。

导入 UIKit 类 ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource {

    var selectedIndex = -1
    var height = 54
    // Data model: These strings will be the data for the table view cells
    let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
    // cell reuse id (cells that scroll out of view can be reused)
    let cellReuseIdentifier = "cell"

    // don't forget to hook this up from the storyboard
    @IBOutlet var tableView: UITableView!
    override func viewDidLoad()
    {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    // number of rows in table view
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 2
    }

    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        // create a new cell if needed or reuse an old one
        let cell:Cell1 = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell1!
        cell.textLabel?.text = "-"
        return cell
    }

    // method to run when table view cell is tapped

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         let cell:Cell1 = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell1!
        if(selectedIndex == indexPath.row)
        {
            height = 216

            cell.textLabel?.text = "+"
        }else{
             height = 54
             cell.textLabel?.text = "/"
        }
        return CGFloat(height)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        print("You tapped cell number \(indexPath.row).")

        if(selectedIndex == indexPath.row)
        {
            selectedIndex = -1
        }
        else
        {

            selectedIndex = indexPath.row
        }
    }
}

【问题讨论】:

  • 只需更改您调用代码的文本字段中的文本即可更改单元格高度 yourTextField.text = "required content"
  • 请帮我编辑我的代码
  • 我无法理解我在哪里更改文本
  • 总结一下您的问题,您是否希望更改特定索引的文本?
  • 是的,我想更改tableview扩展单元格的标签文本

标签: swift uitableview uilabel heightforrowatindexpath


【解决方案1】:

在您的代码中,您尝试在 heightForRowAt 函数中再次使单元格出列,我认为这是您的问题。

所以尝试用下面的代码替换你的 heightForRowAt 函数。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      let cell = tableView.cellForRow(at: indexPath) 
    if(selectedIndex == indexPath.row)
    {
        height = 216

        cell.textLabel?.text = "+"
    }else{
         height = 54
         cell.textLabel?.text = "/"
    }
    return CGFloat(height)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-12
    • 2023-04-06
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多