如何减少 swift 中 tableView 单元格中的代码

how to reduce the code in tableView cell in swift

嘿,我正在学习编码。我创建了一个下载 JSON 数据的应用程序 - covid。 它看起来像这样: enter image description here

我的函数代码(下面的代码)变得非常大。 我怎样才能减少这段代码?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: Cells.covidCell, for: indexPath) as! CovidCell
    
    if inSearchMode == true {
        
        cell.countryLabel.text = filterCovidData[indexPath.row].country
        
        cell.regionLabele.text = filterCovidData[indexPath.row].continent
        cell.casesLabel.text = "Case: \(filterCovidData[indexPath.row].cases!)"
        cell.deathLabel.text = "Death: \(filterCovidData[indexPath.row].deaths!)"
        cell.activelabel.text = "Active: \(filterCovidData[indexPath.row].active!)"
        cell.testsLabel.text = "Test: \(filterCovidData[indexPath.row].tests!)"
        cell.todayCasesInfo.text = "\(filterCovidData[indexPath.row].todayCases!)"
        
        let imageUrl = filterCovidData[indexPath.row].countryInfo?.flag
        
        fetchImage(withUrlString: imageUrl!) { (image) in
            DispatchQueue.main.async {
                cell.countryFlag.image = image
            }
        }
        
    } else {
        cell.countryLabel.text = covidData[indexPath.row].country
        cell.regionLabele.text = covidData[indexPath.row].continent
        cell.casesLabel.text = "Case: \(covidData[indexPath.row].cases!)"
        cell.deathLabel.text = "Death: \(covidData[indexPath.row].deaths!)"
        cell.activelabel.text = "Active: \(covidData[indexPath.row].active!)"
        cell.testsLabel.text = "Test: \(covidData[indexPath.row].tests!)"
        cell.todayCasesInfo.text = "\(covidData[indexPath.row].todayCases!)"
        
        let imageUrl = covidData[indexPath.row].countryInfo?.flag
        
        fetchImage(withUrlString: imageUrl!) { (image) in
            DispatchQueue.main.async {
                cell.countryFlag.image = image
            }
        }
    }
    return cell
}

查看重复代码。 你做同样的事情,除了填充的来源,所以,让我们根据你的需要(inSearchMode)检索模型,然后我们调用相同的代码。

let model = inSearchMode ? filterCovidData[indexPath.row] : covidData[indexPath.row]

cell.countryLabel.text = model.country
    
cell.regionLabele.text = model.continent
cell.casesLabel.text = "Case: \(model.cases!)"
cell.deathLabel.text = "Death: \(model.deaths!)"
cell.activelabel.text = "Active: \(model.active!)"
cell.testsLabel.text = "Test: \(model.tests!)"
cell.todayCasesInfo.text = "\(model.todayCases!)"

let imageUrl = model.countryInfo?.flag

fetchImage(withUrlString: imageUrl!) { (image) in //I'duse a [weak self] here
    DispatchQueue.main.async {
        cell.countryFlag.image = image
    }
}

这是第一步。

你可以在开始时有另一个逻辑:

let arrayToUse = inSearchMode ? filterCovidData : covidData
let model = arrayToUse[indexPath.row]

您还可以在CovidCell

中添加代码
func update(model: ModelThatsInsideCovidData) { 
    countryLabel.text = model.country
        
    regionLabele.text = model.continent
    casesLabel.text = "Case: \(model.cases!)"
    deathLabel.text = "Death: \(model.deaths!)"
    activelabel.text = "Active: \(model.active!)"
    testsLabel.text = "Test: \(model.tests!)"
    todayCasesInfo.text = "\(model.todayCases!)"
    
    let imageUrl = model.countryInfo?.flag

    //Here cell doesn't have that method, should it be accessible?, I'll let you decide.
    fetchImage(withUrlString: imageUrl!) { (image) in
        DispatchQueue.main.async {
            self.countryFlag.image = image
        }
}

然后,在 cellForRowAt:

let model = ...
cell.update(model: model)
return cell