【发布时间】:2016-07-29 16:18:21
【问题描述】:
我有一个名为itemDisplayTable 的UITableView。其中的每个单元格都有一个倒数计时器和一个文本字段。每当值发生变化时,我希望使用来自服务器的数据更新文本字段。所以我每秒使用NSTimer重新加载整个表格视图。
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()
}
表格视图代码:
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”。
【问题讨论】:
-
你从哪里加载你的倒计时计时器的值?
-
我有一个日历按钮,我用它来设置倒数计时器的值。我只希望从服务器更新文本字段值。
-
显示
cellForRowAtIndex代码 -
@Abiranjan 我认为您没有为表格单元维护任何模型。建模将解决您的问题。只需更新您的文本字段值并保持倒计时..
-
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 } }