简体   繁体   English

iOS Swift:当我滚动它时,Tableview数据消失了

[英]iOS Swift : Tableview data disappear when i scroll it

I have started Swift programming and little confuse about the tableview Control. 我已经开始使用Swift编程,并且很少对tableview Control感到困惑。 For implementation i write following code, but the data goes disappear when i scroll it. 为了实现,我编写了以下代码,但滚动时数据会消失。

import UIKit

class ViewController: UIViewController {

    @IBOutlet  var tblView: UITableView

    var Array = ["1","2","3","4","5"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func moveToNextView(sender: AnyObject) {

        var objNextViewController : NextViewController = NextViewController(nibName: "NextViewController", bundle: nil)
        self.navigationController.pushViewController(objNextViewController, animated: true)

    }

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return Array.count
    }

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

        //var cell : UITableViewCell! = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        var cell:UITableViewCell = tblView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        var lblPrint : UILabel! = UILabel(frame: CGRectMake(10, 10, 100, 100))
        lblPrint.text = Array[indexPath.row]
        cell.contentView.addSubview(lblPrint)

        return cell
    }

}

what could be the right method to write for the above code? 什么是正确的方法来编写上述代码?

Thanks 谢谢

The problem is that you are adding var lblPrint : UILabel! 问题是你要添加var lblPrint : UILabel! each time when the cell appears. 每次细胞出现时。

Instead of adding custom label field, you can use UITableViewCell.textLabel . 您可以使用UITableViewCell.textLabel ,而不是添加自定义标签字段。

For example: 例如:

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

    //var cell : UITableViewCell! = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    var cell:UITableViewCell = tblView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.textLabel = Array[indexPath.row]
    return cell
}

Also you can try to create a custom cell with a custom label or you should check if the lblPrint already exists within contentView of cell. 您还可以尝试使用自定义标签创建自定义单元格,或者应检查lblPrint已存在于单元格的contentView中。

For me this problem was fixed by overriding the following method. 对我来说,通过覆盖以下方法解决了这个问题。 I just returned UITableViewAutomaticDimension from the method. 我刚刚从方法中返回了UITableViewAutomaticDimension

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

and set the following property for table view in TableViewController class. 并在TableViewController类中为表视图设置以下属性。

tableView.estimatedRowHeight = 140

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 swift iOS 中垂直滚动时,Tableview 单元格数据正在发生变化 - Tableview cells data is changing when I scroll vertically in swift iOS 当我在TableView中滚动时,UItextField数据消失了— Swift - UItextField data disappears when I scroll through the TableView — Swift 如何避免在TableView iOS Swift中滚动时刷新每个单元格数据? - how to avoid refresh each cell data on scroll in tableview ios swift? 向上滚动后,TableView数据消失 - TableView data disappear after scroll up TableView单元格在滚动时消失 - TableView cell disappear on scroll 在执行分页时,已将新数据添加到tableview中,表明tableview滚动在iOS swift中正在跳跃 - while doing pagination new data has been added to tableview that time tableview scroll is jumping in IOS swift 单击搜索栏上的“取消”时如何重新加载tableview中的所有数据?(IOS / Swift) - How can I reload all of data in tableview when I click cancel on search bar?(IOS/Swift) iOS swift tableview触摸按钮时如何重新加载数据 - iOS swift tableview how can I reload the data when I touch button iOS Swift-TableView数据源-应用关闭时保存数据 - iOS Swift - TableView data source - saving data when app close 如何在 swift IOS 中构造我的数据以在 tableView 中使用? - How can I structure my data in swift IOS to use in tableView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM