zoukankan      html  css  js  c++  java
  • Swift

    Swift - UITableViewCell倒计时重用解决方案

    效果

    源码

    https://github.com/YouXianMing/Swift-Animations

    //
    //  CountDownTimerController.swift
    //  Swift-Animations
    //
    //  Created by YouXianMing on 16/9/4.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    import UIKit
    
    class CountDownTimerController: NormalTitleViewController, UITableViewDelegate, UITableViewDataSource {
    
        var timesArray : [CellDataAdapter]!
        var tableView  : UITableView!
        var timer      : GCDTimer = GCDTimer(inQueue: GCDQueue.mainQueue)
        
        override func setup() {
            
            super.setup()
            
            // Create data source.
            timesArray = [CellDataAdapter]()
            
            func add(title title : String, countdownTime : Int) {
            
                timesArray.append(CountDownTimeCell.Adapter(data: TimeModel(title: title, countdownTime: countdownTime)))
            }
            
            add(title: "YouXianMing", countdownTime: 20034)
            add(title: "Aaron",       countdownTime: 31)
            add(title: "Nicholas",    countdownTime: 1003)
            add(title: "Quentin",     countdownTime: 394)
            add(title: "Samirah",     countdownTime: 345345)
            add(title: "Serafina",    countdownTime: 233)
            add(title: "Shanon",      countdownTime: 4649)
            add(title: "Sophie",      countdownTime: 3454)
            add(title: "Steven",      countdownTime: 54524)
            add(title: "Saadiya",     countdownTime: 235)
            
            // Create TableView.
            tableView                = UITableView(frame: (contentView?.bounds)!)
            tableView.delegate       = self
            tableView.dataSource     = self
            tableView.separatorStyle = .None
            tableView.rowHeight      = 60
            contentView?.addSubview(tableView)
    
            // Register cell.
            CountDownTimeCell.RegisterTo(tableView)
    
            // Timer event.
            weak var wself = self
            timer.event({ 
                
                for (_, dataAdapter) in wself!.timesArray.enumerate() {
                    
                    if let model = dataAdapter.data as? TimeModel {
                    
                        model.countDown()
                    }
                }
                
                DefaultNotificationCenter.PostMessageTo(NotificationEvent.CountDownTimeCellCountDown.Message())
            
                }, timeIntervalWithSeconds: 1.0)
            timer.start()
        }
        
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            
            return timesArray.count
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            
            return tableView.dequeueCellAndLoadContentFromAdapter(timesArray[indexPath.row], indexPath: indexPath)
        }
        
        func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            
            (cell as! CustomCell).display = true
        }
        
        func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            
            (cell as! CustomCell).display = false
        }
    }
  • 相关阅读:
    Codeforces Round #720 (Div. 2) B. Nastia and a Good Array(被坑好几次)1300
    B. Lord of the Values 思维数学建构 附加 英文翻译
    几个i的幂的累加公式1^2+2^2+3^2 2~5
    Codeforces Round #721 (Div. 2)A. And Then There Were K(位运算,二进制) B1. Palindrome Game (easy version)(博弈论)
    洛谷 P2392 kkksc03考前临时抱佛脚, dp / 深搜
    Codeforces Round #719 (Div. 3) C. Not Adjacent Matrix
    Educational Codeforces Round 108 (Div. 2), C map套vector存储
    Day39---->MySQL系列
    Day38——>MySQL
    Day37
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5839799.html
Copyright © 2011-2022 走看看