Skip to content

Instantly share code, notes, and snippets.

@NickHung1982
NickHung1982 / gist:2f8363eb76833309d53ff4f1c8b36a0d
Created July 26, 2017 05:32
demo_dynamic_tableView_cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! CustomCell
let postitem = postAr[indexPath.row]
cell.lb_date.text = postitem.Msg_date
cell.userName.text = postitem.Msg_username
cell.userThumb.sd_setImage(with: URL(string: postitem.Msg_userThumb))
//Here is how we deal with image
DispatchQueue.global(qos: .background).async {
//running background
DispatchQueue.main.async {
// Go back to the main thread to update the UI
}
}
//Tree
class Tree {
var root: Node?
func addValue(_ val: Int) {
let newNode = Node(value: val)
if self.root == nil {
print("root value is \(val)")
self.root = newNode
}else{
root?.addNode(newNode)
@NickHung1982
NickHung1982 / binarytree.swift
Last active September 5, 2017 06:40
Binary Search Tree example
//Tree
class Tree {
var root: Node?
public func addValue(_ val: Int) {
let newNode = Node(value: val)
if self.root == nil {
print("root value is \(val)")
self.root = newNode
}else{
root?.addNode(newNode)
//把每個要比較的字sort後比較, 如果相同就放在一起 直到巡覽完全部再存到outPutAr 最後再一起輸出
func groupAnagrams(_ strs: [String]) -> [[String]] {
var outPutAr = [[String]]()
var compareAr = Set(strs)
while compareAr.count > 0 {
let SameAr = returnSameAr(Array(compareAr))
outPutAr.append(SameAr) //將處理完後的陣列存入 outPutAr
compareAr.subtract(SameAr) //移除已經加過的陣列元素
}
import UIKit
//把每個要比較的字sort後比較, 如果相同就放在一起 直到巡覽完全部再存到outPutAr 最後再一起輸出
func groupAnagrams(_ strs: [String]) -> [[String]] {
var outPutAr = [[String]]()
var compareAr = Set(strs)
while compareAr.count > 0 {
let SameAr = returnSameAr(Array(compareAr))
outPutAr.append(SameAr) //將處理完後的陣列存入 outPutAr
@NickHung1982
NickHung1982 / flatten.swift
Last active September 7, 2017 04:00
flatten nested
var returnList = [Int]()
public func separateArray(_ ar:[Any]) {
for li in ar {
if li is Int {
returnList.append(li as! Int)
}else{
for element in li as! Array<Any> {
if element is Int {
returnList.append(element as! Int)
}else{
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//add UIImageView
customImageView = UIImageView()
customImageView.translatesAutoresizingMaskIntoConstraints = false
customImageView.backgroundColor = UIColor.red
self.addSubview(customImageView)
@NickHung1982
NickHung1982 / cacheImg.swift
Created November 3, 2017 23:49
extension for cache download image
extension UIImageView {
func loadImageUsingCache(withUrl urlString : String) {
let url = URL(string: urlString)
self.image = nil
// check cached image
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
self.image = cachedImage
return
}
@NickHung1982
NickHung1982 / autolayoutCustomCell.swift
Created November 4, 2017 21:24
update with use anchor
//setup centerY with cell
//let labelConstraintCenterY = NSLayoutConstraint(item: customLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
//setup uilabel's height is equal 21
//let labelConstraintHeight = NSLayoutConstraint(item: customLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 21)
//self.addConstraints([labelConstraintCenterY])
//Update use layout Anchor
customLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
customLabel.heightAnchor.constraint(equalToConstant: 21).isActive = true