【发布时间】:2016-12-20 10:29:09
【问题描述】:
我在表格视图中使用自定义单元格我有两个标签一个用于图标(我使用字体中的图标),第二个是单元格的标题
我有一组来自字体的图标,我想在每 30 秒后将它们放在标签中。
例如,我有一个图标数组(message、facebook、twitter、instagram)。我希望标签第一次在 Array 中取第一个,然后每 30 秒取下一个。
socialIcon = ["\u{ea61}","\u{e8c7}","\u{e8c8}","\u{e324}"] // partage + facebook + tweeter + message
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "animate", userInfo: nil, repeats: true)
timer.fire()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
/*let cell = UITableViewCell()
cell.textLabel?.text = "Stretch Header"
cell.selectionStyle = .None*/
// let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! ParametreViewCell
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ParametreViewCell
cell.iconLabel.font = UIFont(name:"innovi", size: 20)
cell.titleLabel.text = self.paramTable[indexPath.row]
if indexPath.row == 0
{
func animate()
{
UIView.transitionWithView(cell.iconLabel,
duration: 1.0,
options: [.CurveEaseInOut],
animations: { () -> Void in
if (self.counter < self.socialIcon.count)
{
self.counter += 1
cell.iconLabel.text = self.socialIcon[self.counter]
print (cell.iconLabel.text )
}
}, completion: nil)
}
}
else
{
cell.iconLabel.text = self.iconTable[indexPath.row]
}
return cell
}
【问题讨论】:
-
使用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ <#code to be executed after a specified delay#> });将动画放入dispatch块内,2秒后执行。 -
代码对我来说没有意义。你为什么叫
timer.fire(),为什么它是在一个类的顶层完成的(这不是有效的Swift)? -
您将无法在
cellForRowAtIndexPath中执行此操作...尝试在willDisplay方法中对其进行动画处理 -
@iphonic 我按照你说的做了,2秒后没有改变,它仍然是我数组中的第一个
-
@Andreas 是的,你是对的,它不是有效的 swift
标签: ios swift uitableview timer uilabel