Ios 如何减少swift中tableView单元格中的代码
嘿,我正在学习编码。我创建了一个下载JSON数据的应用程序——covid。 看起来是这样的: 我的函数中的代码(下面的代码)变得非常大。 如何减少此代码Ios 如何减少swift中tableView单元格中的代码,ios,swift,iphone,programmatically,Ios,Swift,Iphone,Programmatically,嘿,我正在学习编码。我创建了一个下载JSON数据的应用程序——covid。 看起来是这样的: 我的函数中的代码(下面的代码)变得非常大。 如何减少此代码 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: Cells.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
请参阅重复代码。
除了填充的源代码之外,您也可以执行同样的操作,因此,让我们根据您的需要检索模型(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
好的,我在CovidCell func:func update(model:CovidModel){countryLabel.text=model.country…}中创建,现在在表视图中调用update func时出现错误:无法将“[CovidModel]”类型的值转换为预期的参数类型“CovidModel”,让model=[CovidModel]()cell.update(model:model)返回cellNo,它是让model=inSearchMode?filterCovidData:CovidDataThank You ok我在CovidCell func:func update(model:CovidModel){countryLabel.text=model.country…}中创建,现在在表视图中调用update func时出现错误:无法将“[CovidModel]”类型的值转换为预期的参数类型“CovidModel”,让model=[CovidModel]()update(model:model)返回cellNo,它是让model=inSearchMode?filterCovidData:谢谢你