Swift 用嵌套函数实现tableView cellForRowAt
在一个git存储库中,我发现tableView cellForRowAt方法的实现非常奇怪 该方法具有Swift 用嵌套函数实现tableView cellForRowAt,swift,uitableview,swift3,nested-function,Swift,Uitableview,Swift3,Nested Function,在一个git存储库中,我发现tableView cellForRowAt方法的实现非常奇怪 该方法具有configureCell方法的内部实现。这不对吗?比方说。。对于表视图中的1000个单元格,它将创建这个方法大约10万次?有谁能告诉我这是swift的好方法还是不好?我想这是完全错误的 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCel
configureCell
方法的内部实现。这不对吗?比方说。。对于表视图中的1000个单元格,它将创建这个方法大约10万次?有谁能告诉我这是swift的好方法还是不好?我想这是完全错误的
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
func configureVehicleCell(_ object: CustomModel) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCellTableViewCell
if object.hasIssues {
cell.dvirImageView.isHidden = false
}
if object.cmv {
cell.cmvImageView.isHidden = false
}
cell.titleLabel.text = object.name
cell.subtitleLabel.text = object.someName
cell.tag = Int(object.id)
return cell
}
if lastUsedId == nil {
return configureCell(objectArray[(indexPath as NSIndexPath).row])
} else {
if (indexPath as NSIndexPath).section == 0 {
return configureCell(lastUsed!)
} else {
return configureCell(objectsArray[(indexPath as NSIndexPath).row])
}
}
}
我认为应该是这样的:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if lastUsedId == nil {
return configureCell(objectArray[(indexPath as NSIndexPath).row])
} else {
if (indexPath as NSIndexPath).section == 0 {
return configureCell(lastUsed!)
} else {
return configureCell(objectsArray[(indexPath as NSIndexPath).row])
}
}
}
func configureVehicleCell(_ object: CustomModel) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCellTableViewCell
if object.hasIssues {
cell.dvirImageView.isHidden = false
}
if object.cmv {
cell.cmvImageView.isHidden = false
}
cell.titleLabel.text = object.name
cell.subtitleLabel.text = object.someName
cell.tag = Int(object.id)
return cell
}
你有一个误解,它不会创建这个方法大约10万次:它只是一个声明性作用域。@Cœur你能给我链接一篇关于这种行为的文章吗?我什么也找不到。每当我在谷歌上创建问题时,它都会通过我进入完成处理程序。(页面底部)@Cœur基本上与性能有差异吗?如果您将其声明为
private func
,则根本没有性能差异。