Skip to content

Instantly share code, notes, and snippets.

@alcedo
Last active August 29, 2015 14:20
Show Gist options
  • Save alcedo/99a6da20efe6217e0739 to your computer and use it in GitHub Desktop.
Save alcedo/99a6da20efe6217e0739 to your computer and use it in GitHub Desktop.
iOS notes
//
// PhotoTableViewCell.swift
// codepath_wk1e1
//
// Created by Victor Liew on 5/3/15.
// Copyright (c) 2015 alcedo. All rights reserved.
//
import UIKit
class PhotoTableViewCell: UITableViewCell {
var imageHolder: UIImageView?
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
if let url = NSURL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/og.png") {
if let data = NSData(contentsOfURL: url) {
self.imageHolder = UIImageView(image: UIImage(data: data))
self.imageHolder?.contentMode = UIViewContentMode.ScaleAspectFit
}
}
self.contentView.addSubview(self.imageHolder!)
self.imageHolder?.snp_makeConstraints{ (make) -> Void in
make.height.equalTo(300)
make.width.equalTo(300)
make.centerX.equalTo(self.contentView.snp_centerX)
return
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
//
// ViewController.swift
// codepath_wk1e1
//
// Created by Victor Liew on 5/2/15.
// Copyright (c) 2015 alcedo. All rights reserved.
// preview: http://cl.ly/image/1n0X26353p1T
import UIKit
import Snap
import SwiftyJSON
//http://stackoverflow.com/questions/5714528/difference-between-uitableviewdelegate-and-uitableviewdatasource
class PhotosViewController: UIViewController, UITableViewDataSource {
let instagram = InstagramModel()
var mediaArray: JSON?
var tableView: UITableView?
override func loadView() {
super.loadView()
self.view.backgroundColor = UIColor.whiteColor()
self.setupViewStructure()
self.setupConstrain()
}
// MARK: - UI View Creations Goes Here
func setupViewStructure() {
self.tableView = UITableView(frame: CGRectMake(0, 0, 300, 300), style: .Grouped)
if let tv = self.tableView {
self.view.addSubview(tv)
tv.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.view.snp_top)
make.height.equalTo(self.view.snp_height)
make.width.equalTo(self.view.snp_width)
return
}
tv.dataSource = self;
}
}
// MARK: - Constraints
func setupConstrain() {
}
// MARK: - TableView data source
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Header"
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "instagram_photo_cell_identifier"
var cell = self.tableView?.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell?
if(cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
cell!.textLabel?.text = "Row: \(indexPath.row) Section: \(indexPath.section)"
}
return cell!;
}
override func viewDidLoad() {
super.viewDidLoad()
instagram.getPopular({ data in
self.mediaArray = JSON(data!)
})
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// NOTE: just set contentSize to be smaller than its width / height and u will get a scroll view
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(width,height);
// Search bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.tintColor = cell_end_color;
self.tableView.tableHeaderView = searchBar;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
//http://stackoverflow.com/questions/18980955/uisearchdisplaycontroller-displayssearchbarinnavigationbar-positions-the-search
// http://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate/
list of ios notes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment