我需要用滑块中的值更新单元格。滑块正在从解析的JSON数据中提取数据。
如何将滑块中的显示JSON数据添加到tableview单元格?
这是我当前的代码:
import UIKit
class SliderViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var sliderTableView: UITableView!
@IBOutlet weak var styleSlider: UISlider!
@IBOutlet weak var abvSlider: UISlider!
@IBOutlet weak var ibuSlider: UISlider!
@IBOutlet weak var testLabel: UILabel!
@IBAction func styleSlider(_ sender: Any) {
// LABEL CREATED FOR TESTING PURPOSES ONLY
testLabel.text = String(Int(styleSlider.value))
}
var beersMatch = [Beer]()
var filteredBeersMatch = [Beer]()
var filters = [BeerStyle]()
var selectedCells: [Bool] = []
let apiManager = APIManager()
override func viewDidLoad() {
super.viewDidLoad()
apiManager.TapList { (fetchedBeers) in
self.beersMatch = fetchedBeers
var setFilters = Set<BeerStyle>()
for beer in fetchedBeers {
setFilters.insert(beer.StyleName)
}
self.filters = Array(setFilters)
DispatchQueue.main.async {
self.sliderTableView.reloadData()
}
}
}
// MARK: Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return filteredJsonMatch.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier:
"SliderListCell") as? SliderTableViewCell else {
return UITableViewCell()
}
// MARK: Beer name
let beers = filteredJsonMatch[indexPath.row]
cell.sliderMatchLabel.text = beers.BrewName
cell.sliderMatchName.text = beers.BevName
cell.sliderMatchRowNumber.text = jsons.DisplayName
if let IBU = beers.Ibu {
cell.sliderMatchIBU.text = "\(IBU) IBU"
}
if let ABV = beers.Abv {
cell.sliderMatchABV.text = "\(ABV)%"
}
cell.sliderMatchJsonType.text = jsons.StyleName.name
cell.sliderMatchLocation.text = jsons.Location
cell.sliderMatchKegImage.image = jsons.kegPercentageFull()
// MARK: Download image
jsons.downloadImage { (jsonImage) in
DispatchQueue.main.async {
cell.sliderMatchImage.image = jsonImage
}
}
return cell
}
最佳答案
你的filteredJsonMatch
是什么?当你在filters
数组中添加啤酒时,它不应该是filters
吗?
在您的TapList闭包中:
var setFilters = Set<BeerStyle>()
for beer in fetchedBeers {
setFilters.insert(beer.StyleName)
}
self.filters = Array(setFilters)
但在将数据填充到每个单元格时:
let beers = filteredJsonMatch[indexPath.row]
关于json - 如何使用从Swift 3的滑块中选择的JSON数据更新表 View 单元格?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44578696/