gpt4 book ai didi

ios - 更新 UICollectionView 中的 UILabel

转载 作者:行者123 更新时间:2023-11-29 01:23:28 27 4
gpt4 key购买 nike

这是我的 collectionView 代码:

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
let cellWidth = self.view.frame.size.width / 3 - 15
cell.layer.cornerRadius = cellWidth / 15

// Fetch final image - if it exists
if let finalImage = collectedRows[indexPath.row]["picture"] as? PFFile {
finalImage.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {

let imageView = UIImageView(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
imageView.image = UIImage(data: imageData)
imageView.contentMode = UIViewContentMode.ScaleToFill
cell.addSubview(imageView)

if #available(iOS 8.0, *) {
if !UIAccessibilityIsReduceTransparencyEnabled() {
cell.backgroundColor = UIColor.clearColor()

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
//always fill the view
blurEffectView.frame = cell.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

cell.addSubview(blurEffectView)
} else {
let overLayer = UIView(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
overLayer.backgroundColor = UIColor.blackColor()
overLayer.alpha = 0.75
cell.addSubview(overLayer)
}
} else {
// Fallback on earlier versions
let overLayer = UIView(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
overLayer.backgroundColor = UIColor.blackColor()
overLayer.alpha = 0.75
cell.addSubview(overLayer)
}

//Create time Left label

if let sentDate = self.collectedRows[indexPath.row]["sentAt"] as? NSDate {
print("sentDate: \(sentDate)")
let calendar = NSCalendar.currentCalendar()

if #available(iOS 8.0, *) {
let expiryDate = calendar.dateByAddingUnit(.Day, value: 1, toDate: sentDate, options: NSCalendarOptions())
let currentDate = NSDate()
let differenceTime = calendar.components([.Day, .Hour, .Minute, .Second], fromDate: currentDate, toDate: expiryDate!, options: NSCalendarOptions())
print("sendDate2: \(differenceTime)")
let hoursLeft = String(differenceTime.hour)
let minutesLeft = String(differenceTime.minute)
let secondsLeft = String(differenceTime.second)

if (Int(hoursLeft)! <= 0 && Int(minutesLeft)! <= 0 && Int(secondsLeft)! <= 0) {
//remove object from parse
self.collectedRows[indexPath.row].deleteInBackground()
self.collectedRows[indexPath.row].saveInBackground()
} else {
let timerLabel = UILabel(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
timerLabel.font = UIFont(name: "SourceSansPro-Semibold", size: 16)
timerLabel.textAlignment = .Center
timerLabel.textColor = UIColor.whiteColor()
timerLabel.text = "\(hoursLeft):\(minutesLeft):\(secondsLeft)"
cell.addSubview(timerLabel)
cell.bringSubviewToFront(timerLabel)
}
} else {
// Fallback on earlier versions
let timerLabel = UILabel(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
timerLabel.font = UIFont(name: "SourceSansPro-Semibold", size: 16)
timerLabel.textAlignment = .Center
timerLabel.textColor = UIColor.whiteColor()
timerLabel.numberOfLines = 2
timerLabel.text = "Less than \n24 Hours Left"
cell.addSubview(timerLabel)
cell.bringSubviewToFront(timerLabel)

}
}

if #available(iOS 8.0, *) {
let deleteButton = UIButton(frame: CGRectMake(cell.frame.size.width/2, cell.frame.size.height * 0.75, cell.frame.size.width/2, cell.frame.size.height/4))
deleteButton.backgroundColor = UIColor.clearColor()
deleteButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
deleteButton.setTitle("...", forState: .Normal)
deleteButton.titleLabel!.font = UIFont(name: "SourceSansPro-Bold", size: 32)
cell.addSubview(deleteButton)
deleteButton.addTarget(self, action: "removeFromProfile:", forControlEvents: UIControlEvents.TouchUpInside)
} else {
// DO NOT even show button
}
cell.layer.cornerRadius = cell.frame.size.width / 15
cell.layer.masksToBounds = true
}
}
}
}
return cell
}

我正在创建一个 UICollectionViewCell,其中有一张模糊的照片,顶部有一个 UILabel,它是 24 小时倒计时标签。一切都很好。但是,我希望 UILabel 每秒更新一次,以便它不断倒计时。我知道我可以创建一个 NSTimer 来每秒更新整个 UICollectionView (使用 collection.reloadData() )。这显然会导致内存问题并使应用程序崩溃。有谁知道如何仅更新标签?或者有谁知道如何从此方法中提取标签创建,然后将标签应用到 IndexPath 处的适当单元格,然后使用 NSTimer.scheduledWithInterval 调用新方法(标签创建)?标记的部分:

//Create time Left label

这是我每秒都需要更新的唯一部分。

最佳答案

我建议在您的类中创建一个弱属性来保存对 timerLabel 的引用,并在您的 cellForItemAtIndexPath 函数中设置该属性。

您的计时器函数应该能够使用该属性来更新标签,而无需重新加载 Collection View (只要它仍然是 View 层次结构的一部分)。

关于ios - 更新 UICollectionView 中的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319800/

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