swift - 当 swift 中 tableView 单元格的高度扩展时更改标签文本

标签 swift uitableview uilabel heightforrowatindexpath

我是 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
        }
    }
}

最佳答案

在您的代码中,您尝试在 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)
}

关于swift - 当 swift 中 tableView 单元格的高度扩展时更改标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48415826/

相关文章:

swift - 标签中的文本从屏幕溢出 iOS Swift

ios - 不显示 UILabel 宽度的动画减小

ios - 从 Swift 写入 BLE 外设

ios - 如何使用 UIPanGestureRecognizer 设置拖动控件的边界?

ios - iOS Vision 框架面部跟踪的高 CPU 和能源使用

ios - 在动态 TableView 中添加静态单元格

ios - 将此字符串转换为 NSDate

ios - 在 UITableViewController textfield.becomeFirstResponder() 中不滚动 tableview

swift - 如何在 TableView 的一个原型(prototype)单元格内使用两个标签

swift - 当字符串值超过屏幕尺寸时,如何让我的程序自动执行回车?