Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 用嵌套函数实现tableView cellForRowAt_Swift_Uitableview_Swift3_Nested Function - Fatal编程技术网

Swift 用嵌套函数实现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

在一个git存储库中,我发现tableView cellForRowAt方法的实现非常奇怪

该方法具有
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
,则根本没有性能差异。