gpt4 book ai didi

ios - 在不重新加载所有数据的情况下更新 UIImageView

转载 作者:搜寻专家 更新时间:2023-10-31 22:37:39 25 4
gpt4 key购买 nike

这几天一直被这个问题困扰。在触发长按手势之前,我将 ImageView 设置为不可见。但是,我无法全神贯注于如何使 UIImageView 在此之后变得可见。这是在 tableView 的一个 Cell 中。我使用了 reloadData(),但问题是表格丢失了它的位置。当您松开手指时,它会向上滚动。长手势触发数据库端的 bool 值,用于切换 saveShow。加载数据时,查询会检查并显示 UIImageView(如果为真)。所以我相信我必须重新加载数据,因为它是基于查询的..?任何帮助将不胜感激!

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource {

let db = DatabaseController()

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var cityPicker: UIPickerView!

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
cityPicker.delegate = self
cityPicker.dataSource = self

self.tableView.autoresizingMask = UIView.AutoresizingMask.flexibleHeight;
self.db.createTable()

//////////////////////////////////////////////////////////////////
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self as? UIGestureRecognizerDelegate
self.tableView.addGestureRecognizer(longPressGesture)
////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////
Alamofire.request("http://url.com/jsonfile").responseJSON { response in
if (response.result.value != nil) {
let strOutput = JSON(response.result.value ?? "nil")

if let resObj = strOutput.arrayObject {
arrRes2 = resObj as! [[String:AnyObject]]
self.db.addShowsToDatabase(arrRes: arrRes2)
}
}

self.db.deleteOldShows()
self.grabData(id: 1)
self.tableView.reloadData()
}
////////////////////////////////////////////////////////////////////
}

func grabData(id: Int) -> Void {
regionResults = self.db.listAllShows(id: id)
self.tableView.reloadData()
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cities.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cities[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
cheapFix = row + 1
grabData(id: row + 1)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if regionResults.isEmpty {
return 1
} else {
return regionResults.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bandCustomCell") as! BandTableViewCell
if regionResults.isEmpty {
cell.bandPic.image = UIImage(named: "flyer.jpg")
cell.bandSummary?.text = "No Shows Announced!"
cell.bandVenue?.text = "None"
cell.bandDate?.text = "None"
} else {
let show = regionResults[indexPath.row]
//print(try! show.get(Expression<String>("show_summary")))

let strDateFull = try! show.get(Expression<String>("show_date"))
var strDate: Array = ((strDateFull as AnyObject).components(separatedBy: "T"))

// Setting up the date
var dateArr: Array = strDate[0].components(separatedBy: "-")
let dateStr: String = "\(dateArr[1])-\(dateArr[2])-\(dateArr[0])"

// Setting up the time
var timeArr: Array = strDate[1].components(separatedBy: ":")
var timeStr: Int? = Int(timeArr[0])

// Changing military time to standard
if timeStr! > 12 {
timeStr = timeStr! - 12
}
let saved = try! show.get(Expression<Bool>("save_show"))

if (saved == true) {
cell.bandSavedShow.isHidden = false
} else if (saved == false) {
cell.bandSavedShow.isHidden = true
}

cell.bandSummary?.text = "\(try! show.get(Expression<String>("show_summary"))), \(timeStr!):\(timeArr[1])PM"
cell.bandVenue?.text = try! show.get(Expression<String>("venue_name"))
/*if (show["shows_img"] as? String != "null") {
let url = URL(string: show["shows_img"] as! String)
cell.bandPic.kf.setImage(with: url)
} else {*/
cell.bandPic.image = UIImage(named: "flyer.jpg")
//}
cell.bandDate?.text = dateStr

tableView.isScrollEnabled = true

}
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

myIndex2 = indexPath.row
//performSegue(withIdentifier: "segue2", sender: self)
}

@objc func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: touchPoint) {

let show = regionResults[indexPath.row]
let summary = try! show.get(Expression<String>("show_summary"))
let date = try! show.get(Expression<String>("show_date"))
let saved = try! show.get(Expression<Bool>("save_show"))

self.db.toogleSaveShows(summary: summary, date: date, saved: saved)
reload(tableView: self.tableView, indexPath: indexPath)
}
//grabData(id: cheapFix)

}
}

func reload(tableView: UITableView, indexPath: IndexPath) {
//found on stackoverflow
let contentOffset = tableView.contentOffset
tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
tableView.setContentOffset(contentOffset, animated: false)
tableView.endUpdates()
}
}

最佳答案

如下更新 longPressGesture 方法:

let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: touchPoint) {
let cell = tableView.cellForRow(at: indexPath) as? BandTableViewCell;
cell.bandPic.image = ....//provide image here.
}

在这里,不需要重新加载整个表格,而只是访问单元格并在那里设置 ImageView 。

关于ios - 在不重新加载所有数据的情况下更新 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53551803/

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