Ios 关于创建新的tableView单元格,苹果所说的“寄存器”是什么意思?
我正在做一个使用TableView的练习。我注意到在练习期间的一次测试中,他们使用了一种我过去在从故事板实现TableView时不需要的方法。方法是:Ios 关于创建新的tableView单元格,苹果所说的“寄存器”是什么意思?,ios,swift,uitableview,Ios,Swift,Uitableview,我正在做一个使用TableView的练习。我注意到在练习期间的一次测试中,他们使用了一种我过去在从故事板实现TableView时不需要的方法。方法是: func register(AnyClass?, forCellReuseIdentifier: String) 阅读参考页中有关此功能的简短说明后。我很想知道苹果的术语寄存器是什么意思?我有一半的假设是,由于我们目前正在以编程方式进行此练习,因此仅当您以编程方式创建UITableView时才需要此函数。如果这句话不正确,请让我知道,因为我想了
func register(AnyClass?, forCellReuseIdentifier: String)
阅读参考页中有关此功能的简短说明后。我很想知道苹果的术语寄存器是什么意思?我有一半的假设是,由于我们目前正在以编程方式进行此练习,因此仅当您以编程方式创建UITableView时才需要此函数。如果这句话不正确,请让我知道,因为我想了解更多
下面是示例中的代码:
func test_CellForRow_DequesCellFromTableView(){
let mockTableView = MockTableView()
mockTableView.dataSource = sut
mockTableView.delegate = sut
mockTableView.register(ItemCell.self, forCellReuseIdentifier: "ItemCell")
sut?.itemManger?.add(ToDoItem.init(title: "Foo"))
mockTableView.reloadData()
_ = mockTableView.cellForRow(at: IndexPath.init(row: 0, section: 0))
XCTAssertTrue(mockTableView.cellGotDequeed)
}
您正在注册自定义单元类ItemCell,以便作为tableview的单元重用
请参阅:您正在注册自定义单元格类ItemCell,以便作为tableview的单元格重新使用
请参阅:寄存器告诉XCode该单元存在。单元在重用标识符下注册。这是与TableViewCell相对应的唯一字符串,在本例中为ItemCell 通过在单元的属性检查器中填写标识符,也可以在故事板中注册单元
寄存器告诉XCode该单元存在。单元在重用标识符下注册。这是与TableViewCell相对应的唯一字符串,在本例中为ItemCell 通过在单元的属性检查器中填写标识符,也可以在故事板中注册单元 dequeuereusablemethods用于在创建新的可重用单元之前检查是否保留了任何可重用单元。希望你对可重复使用的电池有一个想法 队列为空时会发生什么情况?现在我们确实需要创建一个单元格。我们可以按照两种方法创建一个单元格 手动创建单元格 通过使用有效的xib文件注册单元格自动创建它 方法1 如果手动执行此操作,则必须在dequeueReusableCell检查后检查单元格是否为空。如下图所示
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Reuse an old cell if exist else return nil
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
//check cell is nil if nil you want to allocate it with proper cell
if(cell == nil){
//create cell manually
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CellSubtitle")
}
// do stuff to the cell here
return cell
}
方法2
我们可以像上面那样手动创建单元格,这是完全正确的。但是,如果表格视图可以直接为我们创建单元格,那就很方便了
这样我们就不必从nib加载或实例化它。
为了向xib或类注册单元格,我们使用func registerAnyClass?,forCellReuseIdentifier:String方法。举个例子,
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(MyCell.self, forCellReuseIdentifier: "Cell")
}
// ...
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as MyCell
// no "if" - the cell is guaranteed to exist
// ... do stuff to the cell here ...
cell.textLabel.text = // ... whatever
// ...
return cell
}
dequeuereusablemethods用于在创建新的可重用单元之前检查是否保留了任何可重用单元。希望你对可重复使用的电池有一个想法
队列为空时会发生什么情况?现在我们确实需要创建一个单元格。我们可以按照两种方法创建一个单元格
手动创建单元格
通过使用有效的xib文件注册单元格自动创建它
方法1
如果手动执行此操作,则必须在dequeueReusableCell检查后检查单元格是否为空。如下图所示
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Reuse an old cell if exist else return nil
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
//check cell is nil if nil you want to allocate it with proper cell
if(cell == nil){
//create cell manually
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CellSubtitle")
}
// do stuff to the cell here
return cell
}
方法2
我们可以像上面那样手动创建单元格,这是完全正确的。但是,如果表格视图可以直接为我们创建单元格,那就很方便了
这样我们就不必从nib加载或实例化它。
为了向xib或类注册单元格,我们使用func registerAnyClass?,forCellReuseIdentifier:String方法。举个例子,
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(MyCell.self, forCellReuseIdentifier: "Cell")
}
// ...
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as MyCell
// no "if" - the cell is guaranteed to exist
// ... do stuff to the cell here ...
cell.textLabel.text = // ... whatever
// ...
return cell
}