- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 itemDisplayTable
的 UITableView
。其中的每个单元格都有一个倒计时计时器和一个文本字段。我希望只要值发生变化,文本字段就可以使用来自服务器的数据进行更新。所以我使用 NSTimer
每秒重新加载整个 TableView 。
override func viewDidAppear(animated: Bool) {
var call = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("refresh"), userInfo: nil, repeats: true)
}
func refresh() {
// GET request from the server
itemDisplayTable.reloadData()
}
表格 View 代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (tableView.tag == 1)
{
let cell = tableView.dequeueReusableCellWithIdentifier("collectionCell") as! CollectionCell
cell.category.text = categoryName1[indexPath.row]
setUpScrollView(cell.scrollView,categoryname: categoryName1[indexPath.row])
return cell
}
else
{
let cell = tableView.dequeueReusableCellWithIdentifier("categoryName") as! CategoryTableViewCell
cell.categoryImage .image = UIImage(named: categoryImage[indexPath.row])
cell.categoryName.text = itemname[indexPath.row]
return cell
}
}
func setUpScrollView(scroll : UIScrollView ,categoryname:NSString ){
scroll.subviews.map{ $0.removeFromSuperview() }
items = self.retriveStatus(categoryname as String)
for item in items {
let array = NSBundle.mainBundle().loadNibNamed("collectionView", owner: self, options: nil) as NSArray
let view = array.objectAtIndex(0) as! ItemDisplayView
view.frame = CGRectMake(CGFloat(190 * items.indexOf(item)!)+20, 0, 170 , 250)
view.layer.borderColor=UIColor(red: 231.0/255, green: 231.0/255, blue: 231.0/255, alpha: 1).CGColor
view.layer.borderWidth = 1.0
if item.offerstatus == "active" {
view.invalidateTimer()
view.showOfferTime(item.itemExpiryDate!)
}
view.currentBidPrice.text = item.currentBidPrice // my text field data
scroll.addSubview(view)
}
scroll.contentSize = CGSize(width: (190 * items.count)+20, height: 250)
}
}
tableView 的 Cell 元素类:
class ItemDisplayView: UIView {
@IBOutlet weak var currentBidPrice: UILabel!
@IBOutlet var timerLabel: UILabel!
func showOfferTime(dString:NSString) -> Void {
offerTimer = NSTimer.scheduledTimerWithTimeInterval(1, target:
self, selector: #selector(self.converDatetoTime(_:)),
userInfo:dString, repeats: true)
}
func invalidateTimer() -> Void {
offerTimer.invalidate()
}
func converDatetoTime(timer:NSTimer) -> Void {
let dString = timer.userInfo
let dateAsString = dString
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy, HH:mm:ss"
let date = dateFormatter.dateFromString(dateAsString as! String)
timerLabel.text = self.stringFromTimeInterval(date!.timeIntervalSinceDate(NSDate())) as String
}
func stringFromTimeInterval(interval:NSTimeInterval) -> NSString {
let ti = NSInteger(interval)
//let ms = Int((interval % 1) * 1000)
let seconds = ti % 60
let minutes = (ti / 60) % 60
let hours = (ti / 3600)
if interval>0 {
return NSString(format: "%0.2d:%0.2d:%0.2d",hours,minutes,seconds)
} else {
return "00:00:00"
}
}
问题:文本字段正在完美更新,但由于 tableView 每秒都在重新加载,倒数计时器似乎是“00:00:00”,尽管它正在积极运行。
有什么方法可以单独更新或重新加载单元格内的文本字段,或者避免倒数计时器显示“00:00:00”。
最佳答案
重新加载整个 tableView 不是这样做的方法;它很慢,特别是如果你有一张大 table 。有没有一种方法可以打开一个套接字来监听服务器的变化,并且只更新服务器发送变化时发生变化的特定单元格?您可以使用以下方法重新加载特定的单元格,其中 indexPathsArray
是指向任何需要刷新的单元格的 NSIndexPaths 数组:
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths(indexPathsArray, withRowAnimation: UITableViewRowAnimation.None)
tableView.endUpdates()
我建议为您的 UITableView 维护一个数据模型。现在听起来你的数据状态只保存在 tableView 的单元格中。因此,当您每秒刷新一次 tableView 时,您就丢掉了数据的当前状态。
您需要在 tableView 之外保留一组数据。使用结构将与单个单元格相关的所有数据保持在一起会很有帮助,这样您就不必维护多个数据数组。也许你会有类似的东西:
struct CategoryElement {
var name: String // category's name
var imageName: String // category's image name
var timer: NSTimer // the current timer for that cell item
var value: String // the text field value ?
}
(您可能希望在此处保留更多/更少的数据,但我不能 100% 确定您从问题中所做的事情。)
然后您的 View Controller 将有一个 dataModel 数组,其中包含所有 tableView 单元格的所有信息:var dataModel: [CategoryElement]
。
现在,在 func tableView:cellForRowAtIndexPath
中,您将使用数据模型中的信息创建单元格。由于计时器保存在您的 dataModel 数组中,您将能够获取当前时间并且刷新 tableView 不会重置您的计时器。
if (tableView.tag == 1)
{
let cell = tableView.dequeueReusableCellWithIdentifier("collectionCell") as! CollectionCell
cell.category.text = dataModel[indexPath.row].name
setUpScrollView(cell.scrollView,categoryname: dataModel[indexPath.row].name)
return cell
}
然后在您的 setUpScrollView:categoryname
函数中,您不应该使计时器无效,除非项目的状态发生变化。相反,应该为 ItemDisplayView 的计时器分配 dataModel[indexPath.row].timer
。任何时候您需要停止或重置计时器,都必须通过位于 dataModel[indexPath.row].timer
的数据模型进行更改。
基本上,您的 func tableView:cellForRowAtIndexPath
不应更新数据模型中的任何数据。它应该只是显示它。然后,当您从服务器获取更新时,您将更新您的 dataModel 数组。要在屏幕上显示这些更改,您可以刷新整个表格(但同样,只更新已更改的单元格会更有效)。
我希望这能为您提供一些解决问题的指导。
关于ios - 重新加载表格 View 而不影响单元格中的倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663348/
这是一个非常笼统的问题,我希望我能答对。 我正在研究 SSL/TLS 重新协商并已阅读了一些内容。这是我从阅读中了解到的内容: 从 SSL/TLS 重新协商的角度来看,客户端分为两个主要组,打补丁的和
第一个屏幕是艺术的细节。当我向上滚动时,标题将是 alpha。我点击另一个“艺术”到另一个细节 UI,然后按回到 Previous UI。之前的UI标题是黑色的,怎么变透明了。 布局:
想知道 mv 对基表的影响。它会减慢基表的速度吗?它什么时候开始写入 mv,就像同时写入基表和 mv 一样? 如果我有 local_quorum 的 CL 且 RF=3,客户端是否必须等到写入 mv
似乎在任何地方都找不到太多关于此问题的帮助,所以我想我会在这里尝试。 我正在尝试制作一个简单的 for 循环,当我将鼠标悬停在 html 卡上时,它会隐藏卡中的一些文本。该卡有一个简单的名字和姓氏,我
我有一个程序每帧运行 tick() 方法。我希望一个对象根据设定的重力常数下落,因此我创建了一个 Ball 对象,该对象会将其位置更新为前一帧的位置减去 y 速度。每个刻度 y 速度都会减少重力常数。
我的 KeyHandler 在这里: private void KeyHandler(java.awt.event.KeyEvent evt) {
我有一个方法,其中使用了很多其他类,包括链接列表、队列和堆栈。在我的方法中,我有一个 for 循环,我想在其中弹出堆栈(方便地命名为 s)并将队列(方便地命名为 q)出队到 s1 和 q1。由于某种原
我有一个 JTree 节点数组和另一个自定义对象的相应数组。 我想要什么:当选择 JTree 的节点时,相应对象(其数组中索引与节点数组中所选节点索引相同的对象)的字段填充 JLabels。 我被困在
我知道浏览器完成了处理客户端脚本(Javascript、JQuery 等)的所有工作,但想知道在性能方面是否还有其他重要因素(网络速度、客户端计算机速度、服务器环境) 如果它完全依赖于浏览器(类型和版
我有一个 Android 服务在后台运行,它将使用以下代码: while(true) { ServerSocket server = new ServerSocket(1234); Socke
对JQM有以下疑惑: 1.如果我们在单独的 html 文件中使用重复的 id,对 jquery mobile 有什么影响。 假设我们在单独的 html 文件中有重复的 id,但如果我们不使用该 id
我正在尝试更新两个(inventory、sold)MySQL 表的表库存。 假设我们正在处理的 sku 是 BT888-16 UPDATE inventory JOIN sold ON invento
我使用这种方法来更改我的表格单元格值, 它在 jtable 上改变但在文本文件上没有改变! public class user_AllBooks extends AbstractTableModel
我想在向表中插入数据时创建一个 MYSQL 存储过程,数据也会被插入到其他服务器表中。 我知道这在 ORACLE 数据库中是可能的,但我不知道它是否适用于 MYSQL。 有什么办法吗? 最佳答案 是的
我在 css 方面非常糟糕,只能靠 SO 答案来解决 - 但是我找不到针对这个特定问题的任何解释。 我有一个表单,其中包含一个 textarea 和一个 button(input/submit),仅此
我在一个元素上有动画,但它的移动也会影响 sibling 。如何在不影响兄弟元素的情况下仅在元素上使用动画? 问题示例: function animateSearch() { $('.glyph
我试图在我的 ViewController 中的 UIView 的所有四个边上建立一个阴影 — 在我通过 Xcode 向 UIView 添加约束之前,它工作得很好。我怎样才能使 UIView 的阴影显
自从我使用 JavaScript 以来已经有一段时间了 - 在获得证书之后我开始学习 Perl 并从那时起就一直使用它。我只是想重新开始使用 JS,我已经写了这个,我想说的是,这是一个简单的小脚本,可
我正在处理一个 HTML 元素,我添加了一个复选框,选中后会高亮显示所有文本输入字段。唯一的问题是一些输入字段在表格内,出于某种原因我无法用我的代码影响它们。任何帮助将不胜感激。 相关代码: HTML
我为 String 类创建了一个小扩展,以便方便地从中删除字符。这是它的样子: mutating func drop(characters chars: [String]) { for c i
我是一名优秀的程序员,十分优秀!