Ios 表视图用于出列的单元和初始单元可用性
我对下面的“原型单元”和方法“dequeueReusableCellWithIdentifier”表示怀疑: 假设我在情节提要中创建了一个视图,并将一个表视图附加到它。在第一个场景中,我不会将表视图单元格附加到表视图。然后,我创建以下类来控制我的视图控制器:Ios 表视图用于出列的单元和初始单元可用性,ios,xcode,swift,Ios,Xcode,Swift,我对下面的“原型单元”和方法“dequeueReusableCellWithIdentifier”表示怀疑: 假设我在情节提要中创建了一个视图,并将一个表视图附加到它。在第一个场景中,我不会将表视图单元格附加到表视图。然后,我创建以下类来控制我的视图控制器: import UIKit class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak
import UIKit
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var myArray: String = [String]["One", "Two", "Three"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("testCell") as UITableViewCell!
// I NEED THIS HERE
if (cell == nil) {
cell = UITableViewCell()
}
cell.textLabel!.text = "Test"
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.myArray.count
}
}
对不起,压痕不好
在这种情况下,我需要
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell中的if语句
,以检查方法dequeueReusableCellWithIdentifier
是否返回nil
。如果在故事板中,我将一个表视图单元格(将创建Protype单元格)附加到先前附加的表视图,并将其标识符设置为“testCell”,我是否仍然需要上面的If语句
?我想不是因为我总是有细胞要排队,但我是对的吗?我在这里不考虑我已经创建了允许的最大数量的单元格的情况
谢谢。如果您使用正确的标识符,dequeueReusableCellWithIdentifier:保证从情节提要返回单元格,因此您无需测试cell==nil如果您使用正确的标识符,dequeueReusableCellWithIdentifier:保证从情节提要返回单元格。
import UIKit
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var myArray: String = [String]["One", "Two", "Three"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("testCell") as UITableViewCell!
// I NEED THIS HERE
if (cell == nil) {
cell = UITableViewCell()
}
cell.textLabel!.text = "Test"
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.myArray.count
}
}