我想将一些代码从我的单元格移到它自己的单元格类中,以使其更加整洁。
这是我的代码。
我的词典数组。
var appInfo = [[String:Any]]()
我的手机班。
class resultsCell: UITableViewCell {
@IBOutlet weak var iconPicture: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
func setInfo() {
}
}
我的VC cellForRow。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCell", for: indexPath) as? resultsCell {
let appCell = appInfo[indexPath.row]
let imageUrl = appCell["artwork"] as? String
if imageUrl == nil {
cell.iconPicture.image = #imageLiteral(resourceName: "NoAlbumImage")
}else {
cell.iconPicture.sd_setImage(with: URL(string: "\(imageUrl!)"))
}
cell.titleLabel.text = appCell["name"] as? String
cell.descriptionLabel.text = appCell["desc"] as? String
cell.priceLabel.text = appCell["price"] as? String
let rating = appCell["rating"]
if rating != nil {
cell.ratingLabel.text = "Rating: \((rating!))"
}
return cell
}else {
return UITableViewCell()
}
}
我想将cell.label.text的内容从VC移到单元格类中的set info函数。
这是我的JSON解码和结构。
import Foundation
var appInfo = [[String:Any]]()
class searchFunction {
static let instance = searchFunction()
func getAppData(completion: @escaping (_ finished: Bool) -> ()) {
guard let url = URL(string: BASE_ADDRESS) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let appData = try decoder.decode(Root.self, from: data)
appInfo = []
for app in appData.results {
let name = app.trackName
let desc = app.description
guard let rating = app.averageUserRating else { continue }
let price = app.formattedPrice
let artwork = app.artworkUrl60.absoluteString
let appInd = ["name":name, "desc":desc, "rating":rating, "price":price, "artwork":artwork] as [String : Any]
appInfo.append(appInd)
}
completion(true)
}catch let jsonErr {
print("Error seroalizing json", jsonErr)
}
}.resume()
}
}
结构..
import Foundation
struct Root: Decodable {
var results: [resultsFull]
}
struct resultsFull: Decodable {
var trackName: String
var description: String
var formattedPrice: String
var averageUserRating: Double?
var artworkUrl60: URL
}
答案 0 :(得分:2)
首先,我将字典数组替换为结构数组;这样一来,您就不需要所有的下调工作了:
struct AppInfo {
var artwork: String?
var title: String?
var description: String?
var price: String?
var rating: String?
}
var appInfo = [AppInfo]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCell", for: indexPath) as! ResultsCell
cell.appInfo = self.appInfo[indexPath.row]
return cell
}
然后,您可以使用didSet
使用结构中的值更新单元格
class ResultsCell:UITableViewCell {
@IBOutlet weak var iconPicture: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
var appInfo: AppInfo {
didSet {
iconPicture.image = #imageLiteral(resourceName: "NoAlbumImage")
if let artwork = appInfo.artwork, let artworkURL = URL(string: artwork) {
iconPicture.sd_setImage(with: artworkURL)
}
titleLabel.text = appInfo.title ?? ""
descriptionLabel.text = appInfo.description ?? ""
priceLabel.text = appInfo.price ?? ""
if let rating = appInfo.rating {
ratingLabel.text = "Rating: \(rating)")
} else {
ratingLabel.text = ""
}
}
}
}
答案 1 :(得分:0)
在resultsCell
单元格中写以下函数。
func setInfo(appCell: [String : Any]) {
let imageUrl = appCell["artwork"] as? String
if imageUrl == nil {
iconPicture.image = #imageLiteral(resourceName: "NoAlbumImage")
}else {
iconPicture.sd_setImage(with: URL(string: "\(imageUrl!)"))
}
titleLabel.text = appCell["name"] as? String
descriptionLabel.text = appCell["desc"] as? String
priceLabel.text = appCell["price"] as? String
let rating = appCell["rating"]
if rating != nil {
ratingLabel.text = "Rating: \((rating!))"
}
}
现在将您的数组值传递给下面的cellForRowAt
方法。
let appCell = appInfo[indexPath.row]
cell.setInfo(appCell: appCell)
答案 2 :(得分:0)
class ResultsCell: UITableViewCell {
@IBOutlet weak var iconPicture: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
func setInfo(_ appCell: [String:Any], inIndexPath: IndexPath, _ cntrl: UIViewController) {
let imageUrl = appCell["artwork"] as? String
if imageUrl == nil {
iconPicture.image = #imageLiteral(resourceName: "NoAlbumImage")
}else {
iconPicture.sd_setImage(with: URL(string: "\(imageUrl!)"))
}
titleLabel.text = appCell["name"] as? String
descriptionLabel.text = appCell["desc"] as? String
priceLabel.text = appCell["price"] as? String
let rating = appCell["rating"]
if rating != nil {
ratingLabel.text = "Rating: \((rating!))"
}
}
}
,然后在tableView的委托中写:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(
withIdentifier: "your Results Cell ID from storyboard",
for: indexPath) as? resultsCell {
let appCell = appInfo[indexPath.row]
cell.setInfo(appCell, inIndexPath: indexPath, self)
return cell
}else {
return UITableViewCell()
}
}
进一步,只需删除词典数组即可,最好使用structs
中的models
。