我搜索了我所知道的每个来源,以寻求这个问题的帮助。我希望在一段时间过后,使tableview中的各个行消失。即使应用程序未打开,我希望在计时器到达零时立即删除行。我一直试图将每个帖子安排到一个带有计时器配对的字典中,以便在时间发生时处理行删除。我看了这篇文章的指导但没有解决方案。 Swift deleting table view cell when timer expires
这是我处理tableview和计时器的代码:
var nextID: String?
var postsInFeed = [String]()
var postTimer = [Timer: String]()
var timeLeft = [String: Int]()
(在视图中加载)
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)
}
}
在tableviewcell中:
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
}
任何帮助都会非常感激!
答案 0 :(得分:0)
当您的应用未运行或暂停时,定时器不会运行。
您应该重新编写代码以在启动计时器时保存日期(到UserDefaults),然后每次计时器触发时,将当前日期与保存日期进行比较。使用经过的时间量来确定要删除的表视图数据模型中的条目数。 (如果您在暂停/未运行时经过了多个计时器时间段。)
您应该在您的app委托中实施applicationDidEnterBackground()
(或订阅相应的通知,UIApplicationDidEnterBackground
)并停止计时器。
然后实现app delegate applicationDidBecomeActive()
方法(或为UIApplicationDidBecomeActive
通知添加通知处理程序),并在该方法中检查已经过的时间量,更新表视图的数据模型,如果您删除了任何条目,请告诉表视图重新加载。然后,当应用程序在前台运行时,最后重新启动计时器以更新表视图。