使用嵌套函数实现tableView cellForRowAt

时间:2017-06-16 08:04:53

标签: swift uitableview swift3 nested-function

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

该方法具有configureCell方法的内部实现。这不是错的吗?让我们说..对于表视图中的1000个单元格 - 它将创建此方法大约1000万次?任何人都可以告诉我它是否是快速的好方法?我想这是完全错误的。

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
            }

0 个答案:

没有答案