gpt4 book ai didi

ios - 在 Swift 4 中滚动 UITableView 时如何更新 UISlider 值

转载 作者:行者123 更新时间:2023-12-01 19:44:28 25 4
gpt4 key购买 nike

当我设置 slider 值时,请参阅此屏幕短 enter image description here

然后 tableview向下滚动然后更改值,请参阅此屏幕短 enter image description here

问题是当我向下滚动时值会发生变化,我已经看到了很多答案,但是在 Objective-C 中,请在 Swift 中帮助我解决这个问题。我正在使用 Xcode 9.2 和 Swift 4 非常感谢!

在这里,我将使用 CoreData,但我只想要 Slider 问题!

class ViewController: UIViewController {

@IBOutlet var tableView: UITableView!
var arrData = [String]()
var studentData = [Student]()
override func viewDidLoad() {
super.viewDidLoad()
studentData = DatabaseHelper.shareInstance.getAllData()
arrData = [
"https://is3-ssl.mzstatic.com/image/thumb/Music1/v4/53/58/26/5358265a-e225-2cfb-61da-e6cb68ea5866/source/100x100bb.jpg","https://is3-ssl.mzstatic.com/image/thumb/Music118/v4/3b/cf/4c/3bcf4c0d-a040-3d74-81e9-6ca62c3664ab/source/100x100bb.jpg",
"https://is2-ssl.mzstatic.com/image/thumb/Music/v4/4f/a6/a3/4fa6a31a-26f0-c025-4f1c-a20da9dd4a32/source/100x100bb.jpg","https://is5-ssl.mzstatic.com/image/thumb/Music5/v4/eb/43/a3/eb43a360-d92e-7dbd-feb8-73b8496ba66f/source/100x100bb.jpg"
]
for arr in arrData{
DatabaseHelper.shareInstance.save(object: arr)
self.tableView.reloadData()
}
}

}

extension ViewController: UITableViewDelegate, UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return studentData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.studentData = studentData[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}

//TableViewCell
class TableViewCell: UITableViewCell {


@IBOutlet var slider: UISlider!
@IBOutlet var img: UIImageView!
var studentData:Student!{
didSet{
let url = ImageResource(downloadURL: URL(string: studentData.photo!)!, cacheKey: studentData.photo)
img.kf.setImage(with: url)
slider.value = studentData.valueslider
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
DatabaseHelper.shareInstance.saveValue(object: slider.value)
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}


@IBAction func sliderBtnClick(_ sender: UISlider) {
img.alpha = CGFloat(slider.value)
}


}

//数据库助手
class DatabaseHelper{
static let shareInstance = DatabaseHelper()

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

func save(object:String){
let entity = NSEntityDescription.insertNewObject(forEntityName: "Student", into: context) as! Student
entity.photo = object
do{
try context.save()
}catch{
print("Data not save")
}
}

func saveValue(object:Float){
let entity = NSEntityDescription.insertNewObject(forEntityName: "Student", into: context) as! Student
entity.valueslider = object
do{
try context.save()
}catch{
print("Data not save")
}
}

func getAllData() -> [Student]{
var arrData = [Student]()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Student")
do{
arrData = try context.fetch(fetchRequest) as! [Student]
}catch{
print("Data not get")
}
return arrData
}
}

数据 CoreData 类

进口基金会
导入核心数据

@objc(学生)
公共(public)课学生:NSManagedObject {

}
import Foundation
import CoreData

extension Student {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Student> {
return NSFetchRequest<Student>(entityName: "Student")
}

@NSManaged public var photo: String?
@NSManaged public var valueslider: Float

}

最佳答案

protocol SliderDelegate: class {
func sliderValueChanged(withValue: Int, cell: TableViewCell)
}

class TableViewCell: UITableViewCell {
weak var delegate: SliderDelegate?
@IBAction func sliderBtnClick(_ sender: UISlider) {
img.alpha = CGFloat(slider.value)
delegate?.sliderValueChanged(withValue: slider.value, cell: self)
}

}

在您的 View Controller 的 TableView 数据源方法中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.delegate = self
cell.studentData = studentData[indexPath.row]
return cell
}

并符合 Sliderdelegate
extension ViewController: SliderDelegate {
func sliderValueChanged(withValue: Int, cell: TableViewCell) {
guard let indexPath = yourTable.indexPath(for: cell) else {
return
}
// Save Slider value to database (Database.save... your logic to update the slider value for the data at this indexPath, goes here) and then reload data like follows
yourTable.reloadRows(at: [indexPath], with: .none)
}

关于ios - 在 Swift 4 中滚动 UITableView 时如何更新 UISlider 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50168196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com