Last active
November 11, 2015 08:26
-
-
Save 178inaba/c2896e99964d39ef0263 to your computer and use it in GitHub Desktop.
table view display update of the loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ViewController.swift | |
| // SwiftCollatz | |
| // | |
| // Created by 178inaba on 2015/10/18. | |
| // Copyright © 2015年 178inaba. All rights reserved. | |
| // | |
| import UIKit | |
| class ViewController: UIViewController | |
| ,UITableViewDataSource | |
| //,UITableViewDelegate | |
| { | |
| @IBOutlet weak var targetNumField: UITextField! | |
| @IBOutlet weak var tableView: UITableView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| tableView.dataSource = self | |
| self.texts = ["Sunday"] | |
| var baseSec = 0.0 | |
| for _ in 0...100 { | |
| let delay = baseSec * Double(NSEC_PER_SEC) | |
| testLoop(delay) | |
| baseSec += 1.0 | |
| } | |
| } | |
| func testLoop(delay: Double) { | |
| let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) | |
| dispatch_after(time, dispatch_get_main_queue(), { | |
| self.texts.append("a") | |
| self.tableView.reloadData() | |
| }) | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| @IBAction func tapCalc(sender: UIButton) { | |
| texts = ["Sunday"] | |
| for _ in 0...10 { | |
| texts.append("a") | |
| tableView.reloadData() | |
| sleep(1) | |
| } | |
| // calcCollatz(UInt64(targetNumField.text!)!) | |
| } | |
| func calcCollatz(num: UInt64) { | |
| print(num) | |
| if num % 2 == 1 && num > 1 { | |
| calcCollatz(3 * num + 1) | |
| } else if num % 2 == 0 { | |
| calcCollatz(num / 2) | |
| } | |
| } | |
| // display texts | |
| var texts = ["Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] | |
| // cell count | |
| func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| return texts.count | |
| } | |
| // update cell value | |
| func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
| let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") | |
| cell.textLabel?.text = texts[indexPath.row] | |
| return cell | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment