gpt4 book ai didi

ios - 时间到期后从 Tableview 中删除行

转载 作者:行者123 更新时间:2023-11-30 12:13:18 24 4
gpt4 key购买 nike

我已经搜索了我所知道的所有资源来寻求有关此问题的帮助。我想让 TableView 中的各个行在一定时间到期后消失。即使应用程序未打开,我也希望在计时器达到零时立即删除行。我一直在尝试将每个帖子排列到带有计时器配对的字典中,以在时间发生时处理行删除。我查看了这篇文章以寻求指导,但没有解决方案。 Swift deleting table view cell when timer expires .

这是我处理表格 View 和计时器的代码:

var nextID: String?
var postsInFeed = [String]()
var postTimer = [Timer: String]()
var timeLeft = [String: Int]()

( View 中已加载)

DataService.ds.REF_FEED.observe(.value, with: { (snapshot) in
self.posts = []
self.postsInFeed = []
self.nextID = nil

if let snapshot = snapshot.children.allObjects as? [DataSnapshot] { //Gets us into our users class of our DB
for snap in snapshot { // Iterates over each user
if let postDict = snap.value as? Dictionary<String, Any> { // Opens up the dictonary key value pairs for each user.
let key = snap.key //
let post = Post(postID: key, postData: postDict) // Sets the properties in the dictionary to a variable.
self.posts.append(post) // appends each post which contains a caption, imageurl and likes to the empty posts array.


self.nextID = snap.key
let activeID = self.nextID
self.postsInFeed.append(activeID!)

print(self.postsInFeed)
print(activeID!)


}

}

}
self.tableView.reloadData()

})

//////////////////////////////////////////////////////////////////////////////////////////////
// Sets up our tableview
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let post = posts[indexPath.row] // We get our post object from the array we populate when we call the data from the database up above.

if let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell") as? TableViewCell { //Specifies the format of the cell we want from the UI

//cell.cellID = self.postsInFeed[indexPath.row]

        cell.cellID = self.postsInFeed[indexPath.row]


cell.homeVC = self


if let img = HomeVC.imageCache.object(forKey: post.imageUrl as NSString){
cell.configureCell(post: post, img: img as? UIImage)
print(postTimer)
print(self.timeLeft)

} else {

cell.configureCell(post: post)
print(postTimer)
print(self.timeLeft)
}


return cell


} else {
return TableViewCell()
}
}

func handleCountdown(timer: Timer) {
let cellID = postTimer[timer]

// find the current row corresponding to the cellID
let row = postsInFeed.index(of: cellID!)

// decrement time left
let timeRemaining = timeLeft[(cellID!)]! - 1
timeLeft[cellID!] = timeRemaining
if timeRemaining == 0 {
timer.invalidate()

postTimer[timer] = nil
postsInFeed.remove(at: row!)
tableView.deleteRows(at: [IndexPath(row: row!, section: 0)], with: .fade)

} else {
tableView.reloadRows(at: [IndexPath(row: row!, section: 0)], with: .fade)
}
}

在表格 View 单元格中:

weak var homeVC: HomeVC?
var cellID: String!

func callTime() {
homeVC?.timeLeft[cellID] = 25
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(homeVC?.handleCountdown(timer:)), userInfo: nil, repeats: true)
homeVC?.postTimer[timer] = cellID

}

任何帮助将不胜感激!

最佳答案

当您的应用未运行或暂停时,计时器不会运行。

您应该修改代码,在启动计时器时保存一个日期(到 UserDefaults),然后每次计时器触发时,将当前日期与保存的日期进行比较。使用耗时来决定 TableView 数据模型中要删除的条目数。 (如果您暂停/未运行时超过一个计时器周期已过。)

您应该在应用委托(delegate)中实现 applicationDidEnterBackground()(或订阅等效的通知 UIApplicationDidEnterBackground)并停止计时器。

然后实现应用程序委托(delegate) applicationDidBecomeActive() 方法(或为 UIApplicationDidBecomeActive 通知添加通知处理程序),并在该方法中检查已执行的时间量已过去,更新 TableView 的数据模型,并告诉 TableView 重新加载(如果您删除了任何条目)。然后最后重新启动计时器以在应用程序在前台运行时更新表格 View 。

关于ios - 时间到期后从 Tableview 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45763847/

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