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

y3bcpkx1  于 2023-05-02  发布在  iOS
关注(0)|答案(1)|浏览(204)

我正在学习编程。我创建了一个下载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
}
gk7wooem

gk7wooem1#

看看重复的代码。除了填充的源代码之外,您也可以执行相同的操作,因此,让我们根据您的需要检索模型(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

相关问题