[英]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.