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



我正在学习编程。我创建了一个下载JSON数据的应用程序- covid。它是这样的:输入图片描述

函数中的代码(下面的代码)变得非常大。如何减少这些代码?

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

最新更新