Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2016 17:09
Show Gist options
  • Save anonymous/79eb4b2506889929cfe92dfcab3731c4 to your computer and use it in GitHub Desktop.
Save anonymous/79eb4b2506889929cfe92dfcab3731c4 to your computer and use it in GitHub Desktop.
class RecipesTableViewController: UITableViewController {
@IBOutlet var myTableView: UITableView!
@IBOutlet var menuButton:UIBarButtonItem!
//@IBOutlet weak var segment: UISegmentedControl!
@IBOutlet var segmentControl : ADVSegmentedControl!
var pageMenu : CAPSPageMenu?
var recipes:[Recipe] = [
Recipes(name: "巧克力手工餅乾", type: "餅乾", material: "1.無鹽奶油\n2.低筋麵粉\n3.可可粉\n4.蛋\n5.細砂糖\n6.泡打粉(可省)", image: "ChocolateCookie.jpg", isVisited: false, amount:"150g\n275g\n25g\n1顆\n100g\n1/2小匙"),
Recipe(name: "辣味雞肉義大利麵", type: "麵食", material: "1.義大利麵\n2.雞胸肉\n3.洋蔥\n4.白酒\n5.蘆筍\n6.鴻喜菇\n7.玉米筍\n8.甜椒\n9.鮮奶油\n10.奶油\n11.蒜碎\n12.花椒油\n13.義大利香料", image: "pasta.jpg", isFavor: false, amount:"180g\n50g\n15g\n50g\n15g\n10g\n10g\n150g\n5g\n10g\n5g\n20g\n2g", userPhoto:"Adrian Hsiao.jpg", userName:"Adrian Hsiao" ),
//...other recipes datas
]
var noodles:[Recipe] = [
Recipe(name: "辣味雞肉義大利麵", type: "麵食", material: "1.義大利麵\n2.雞胸肉\n3.洋蔥\n4.白酒\n5.蘆筍\n6.鴻喜菇\n7.玉米筍\n8.甜椒\n9.鮮奶油\n10.奶油\n11.蒜碎\n12.花椒油\n13.義大利香料", image: "pasta.jpg", isFavor: false, amount:"180g\n50g\n15g\n50g\n15g\n10g\n10g\n5g\n150g\n10g\n5g\n20g\n2g", userPhoto:"Adrian Hsiao.jpg", userName:"Adrian Hsiao" ),
Recipe(name: "焗烤海鮮筆管麵", type: "麵食", material: "1.筆管麵\n 2.洋蔥\n3.蛤蜊\n4.透抽\n5.鮮蝦\n6.蒜頭\n7.水\n8.康寶濃湯粉\n9.鹽\n10.起司絲\n11.煮熟綠花椰菜", image: "pasta2.jpg", isFavor: false, amount:"1/3包\n1顆\n適量\n適量\n適量\n三小瓣\n1杯半\n半包\n少許\n適量\n適量", userPhoto:"阿渝師~美味廚房.jpg", userName:"阿渝師~美味廚房" )
]
var snacks:[Recipe] = [
Recipe(name: "巧克力手工餅乾", type: "甜點", material: "1.無鹽奶油 150g\n2.低筋麵粉 275g\n3.可可粉 25g\n4.蛋 1顆\n5.細砂糖 100g\n6.泡打粉(可省) 1/2小匙", image: "ChocolateCookie.jpg", isFavor: false, amount:"", userPhoto:"小璧.jpg", userName:"小璧" ),
Recipe(name: "巧克力戚風蛋糕(6吋)", type: "甜點", material: "1.低筋麵粉\n2.無糖可可粉\n3.植物油\n4.牛奶\n5.蛋黃\n6.蛋白\n7.細砂糖\n8.蛋糕裝飾(奶油)", image: "chocolateCake.jpg", isFavor: false, amount:"45g\n15g\n30g\n60g\n3個\n3個\n45g\n適量", userPhoto:"維尼Winnie.jpg", userName:"維尼Winnie" )
]
override func viewDidLoad() {
super.viewDidLoad()
segmentControl.items = ["首頁", "麵食", "點心", "飯類", "孕婦"]
segmentControl.font = UIFont(name: "Avenir-Black", size: 12)
segmentControl.borderColor = UIColor(white: 1.0, alpha: 0.3)
segmentControl.selectedIndex = 0
segmentControl.addTarget(self, action: #selector(RecipesTableViewController.changePage(_:)), forControlEvents: .ValueChanged)
self.loadData()
} //end viewDidLoad
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
//下兩個為建立tableView cell時必須建立的兩個function
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section
// if segment.selectedSegmentIndex == 1 {
//}
if segmentControl.selectedIndex == 0 {
return self.recipes.count
}
if segmentControl.selectedIndex == 1 {
return self.noodles.count
}
if segmentControl.selectedIndex == 2 {
return self.snacks.count
}
if segmentControl.selectedIndex == 3 {
return self.noodles.count
}
if segmentControl.selectedIndex == 4 {
return self.noodles.count
}
return self.recipes.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell
if segmentControl.selectedIndex == 0 {
let recipe = recipes[indexPath.row]
let aux:AnyObject = data[indexPath.row]
let table_recipeName = aux["recipeName"]
cell.nameLabel.text = table_recipeName as? String
let table_recipeType = aux["recipeType"]
cell.typeLabel.text = table_recipeType as? String
let table_recipeImage = aux["recipeImage"]
cell.thumbnailImageView.image = UIImage(named: table_recipeImage as! String)
let table_recipeIsFavor = aux["recipeIsFavor"]
cell.favorIconImageView.hidden = !recipe.isFavor
let table_recipeUserPhoto = aux["recipeUserPhoto"]
cell.userPhoto.image = UIImage(named: table_recipeUserPhoto as! String)
let table_recipeUserName = aux["recipeUserName"]
cell.userName.text = table_recipeUserName as? String
}
if segmentControl.selectedIndex == 1 {
let noodle = noodles[indexPath.row]
cell.favorIconImageView.hidden = !noodle.isFavor
cell.nameLabel.text = noodle.name
cell.thumbnailImageView.image = UIImage(named: noodle.image)
cell.typeLabel.text = noodle.type
cell.userPhoto.image = UIImage(named: noodle.userPhoto)
cell.userName.text = noodle.userName
}
if segmentControl.selectedIndex == 2 {
//...
}
if segmentControl.selectedIndex == 3 {
//...
}
if segmentControl.selectedIndex == 4 {
//...
}
return cell
}
var category: Int = Int()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showRecipesDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! DetailViewController
if segmentControl.selectedIndex == 0 {
destinationController.recipes = recipes[indexPath.row]
destinationController.category = self.category
print(category)
}
if segmentControl.selectedIndex == 1 {
destinationController.noodles = noodles[indexPath.row]
destinationController.category = self.category
print(category)
}
if segmentControl.selectedIndex == 2 {
//...
}
if segmentControl.selectedIndex == 3 {
//...
}
if segmentControl.selectedIndex == 4 {
//...
}
}
}
}
//**********************SQLite********************************//
var statement = COpaquePointer()
var data: [[String: AnyObject]] = []
var noodeleData: [[String: AnyObject]] = []
func loadData() {
let db_path = "/Users/huangpinquan/Desktop/Recipes/Recipes/Recipes.sqlite"
var db = COpaquePointer()
let status = sqlite3_open(db_path,&db)
if (status == SQLITE_OK) {
print("Open the sqlite success!\n")
}else {
print("Open the sqlite failed!\n")
}
let query_stmt = "SELECT * FROM recipe WHERE type:'麵食' "
//let query_stmt = "SELECT * FROM recipe"
if(sqlite3_prepare_v2(db , query_stmt, -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
var recipeArray: [String: AnyObject] = [:]
let recipeName = sqlite3_column_text(statement, 0)
let recipe_name = String.fromCString(UnsafePointer<CChar>(recipeName)) ?? ""
let recipeType = sqlite3_column_text(statement, 1)
let recipe_type = String.fromCString(UnsafePointer<CChar>(recipeType)) ?? ""
let recipeImage = sqlite3_column_text(statement, 2)
let recipe_image = String.fromCString(UnsafePointer<CChar>(recipeImage)) ?? ""
let recipeIsFavor = sqlite3_column_int(statement, 3)
let recipe_isFavor = Int(recipeIsFavor)
let recipeUserPhoto = sqlite3_column_text(statement, 4)
let recipe_userPhoto = String.fromCString(UnsafePointer<CChar>(recipeUserPhoto)) ?? ""
let recipeUserName = sqlite3_column_text(statement, 5)
let recipe_userName = String.fromCString(UnsafePointer<CChar>(recipeUserName)) ?? ""
recipeArray["recipeName"] = recipe_name
recipeArray["recipeType"] = recipe_type
recipeArray["recipeImage"] = recipe_image
recipeArray["recipIsFavor"] = recipe_isFavor
recipeArray["recipeUserPhoto"] = recipe_userPhoto
recipeArray["recipeUserName"] = recipe_userName
data.append(recipeArray)
}
sqlite3_finalize(statement)
}else {
print("read the sqlite data failed")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment