Skip to content

Instantly share code, notes, and snippets.

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 / 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{
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
//把每個要比較的字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) //移除已經加過的陣列元素
}
@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)
//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)
DispatchQueue.global(qos: .background).async {
//running background
DispatchQueue.main.async {
// Go back to the main thread to update the UI
}
}
@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