【问题标题】:how can i change UILabel text in tableView with delay如何延迟更改 tableView 中的 UILabel 文本
【发布时间】: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(), ^{ &lt;#code to be executed after a specified delay#&gt; });将动画放入dispatch块内,2秒后执行。
  • 代码对我来说没有意义。你为什么叫timer.fire(),为什么它是在一个类的顶层完成的(这不是有效的Swift)?
  • 您将无法在 cellForRowAtIndexPath 中执行此操作...尝试在 willDisplay 方法中对其进行动画处理
  • @iphonic 我按照你说的做了,2秒后没有改变,它仍然是我数组中的第一个
  • @Andreas 是的,你是对的,它不是有效的 swift

标签: ios swift uitableview timer uilabel


【解决方案1】:
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    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
    {
        cell.iconLabel.text = self.socialIcon[0]
        NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(animate), userInfo: nil, repeats: true)

    }
    else
    {
        cell.iconLabel.text = self.iconTable[indexPath.row]
    }


    return cell
}

func animate() {
    var indexPath = NSIndexPath.init(forRow: 0, inSection: 0)
    if let ifExists = tableView.indexPathsForVisibleRows?.contains(indexPath)
        where ifExists {
        let customCell = tableView.cellForRowAtIndexPath(indexPath) as! ParametreViewCell
        UIView.transitionWithView(customCell.iconLabel,
                                  duration: 1.0,
                                  options: [.CurveEaseInOut],
                                  animations: { () -> Void in
                                    if (self.counter < self.socialIcon.count )
                                    {
                                        customCell.iconLabel.text = self.socialIcon[self.counter]
                                        self.counter += 1

                                    }
                                    else
                                    {
                                         self.counter = 0
                                    }
            }, completion: nil)
    }
}

【讨论】:

  • 你可以完全避免if...为什么不使用...customCell.iconLabel.text = self.socialIcon[self.counter] self.counter += 1 self.counter %= self.socialIcon.count
猜你喜欢
  • 1970-01-01
  • 2016-05-29
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2015-11-08
  • 1970-01-01
相关资源
最近更新 更多