swift / JSON取得&リスト表示&タイトルクリックでURLに飛ぶ

import UIKit
import Alamofire

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet var table: UITableView!
    var newsDataArray = NSArray();
    func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int{
        return newsDataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)->UITableViewCell{
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
        let newsDic = newsDataArray[indexPath.row] as! NSDictionary
        cell.textLabel?.text = newsDic["title"] as? String
        cell.textLabel?.numberOfLines = 3
        cell.detailTextLabel?.text = newsDic["publishedDate"] as? String
        return cell
    }

    func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath){
        let newsDic = newsDataArray[indexPath.row] as! NSDictionary;
        let newsUrl = newsDic["unescapedUrl"] as! String;
        let url = NSURL(string:newsUrl);
        let app = UIApplication.sharedApplication();
        app.openURL(url!);
    }
    
    override func viewDidLoad() {
        table.dataSource = self;
        table.delegate = self;
        super.viewDidLoad()
        getRSS();
    }

    func getRSS(){
        let requestUrl = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&topic=p&hl=ja&rsz=8";
        Alamofire.request(.GET, requestUrl, parameters: nil).responseJSON {
            response in
            if let json = response.result.value {
                let jsonDic = json as! NSDictionary
                let responseData = jsonDic["responseData"] as! NSDictionary
                self.newsDataArray = responseData["results"] as! NSArray
                self.table.reloadData()
            }
        }
    }

swift / jsonをhttp経由で取得

Download

setting

  • general > embed Binary で[+]で追加。
  • [Alamofile iOS]を選択して追加

code

        let requestUrl = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&topic=p&hl=ja&rsz=8";

        Alamofire.request(.GET, requestUrl, parameters: nil).responseJSON {
            response in
            if let json = response.result.value {
                print(json)
            }
        }

swift / UIViewを左右に揺らす

       var transform:CGAffineTransform = CGAffineTransformIdentity
        let duration:Double = 0.5
        transform = CGAffineTransformMakeRotation(CGFloat(0.25*M_PI))
        UIView.animateWithDuration(duration,animations:{()-> Void in
            sender.transform = transform
            })
            { (Bool)-> Void in
             UIView.animateWithDuration(duration,animations:{ () -> Void in
                sender.transform = CGAffineTransformIdentity
                }){ (Bool) -> Void in
            }
        }

swift / UIButtonをコードでグラデーションかけたり、角丸にしたり

import UIKit
import QuartzCore

...
        let gradient = CAGradientLayer();
        gradient.frame = button.bounds;
        let arryColors = [
            colorWithRGBHex(0xFFFFFF,alpha:1.0).CGColor as AnyObject,
            colorWithRGBHex(0xCCCCCC,alpha:1.0).CGColor as AnyObject
        ];
        gradient.colors = arryColors;
        button.layer.insertSublayer(gradient, atIndex:0)
        
        //ボタンを角丸に
        button.layer.masksToBounds = true;
        button.layer.cornerRadius = 5.0;
        button.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal);

swift / ColorコードをUIColorに変換

    func colorWithRGBHex(hex:Int,alpha: Float = 1.0) -> UIColor{
        let r = Float((hex >> 16) & 0xFF);
        let g = Float((hex >> 8) & 0xFF);
        let b = Float((hex ) & 0xFF);
        return UIColor(
            red: CGFloat(r / 255.0),
            green: CGFloat(g / 255.0),
            blue: CGFloat(b / 255.0),
            alpha: CGFloat(alpha))
    }

使い方

        let arryColors = [
            colorWithRGBHex(0xFFFFFF,alpha:1.0).CGColor as AnyObject,
            colorWithRGBHex(0xCCCCCC,alpha:1.0).CGColor as AnyObject
        ];