Last active
July 17, 2017 09:31
-
-
Save sunnyleeyun/457c936652e351ab2a3dfadbaf59ac04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a.原本的ViewController(MainTableViewController) 部份截取,詳細請至github下載完整專案 | |
func fetchMealsList(){ //將這個function放至viewDidLoad中 | |
//之前提到的.childAdded是專門處理List | |
refHandle = databaseRef.child("Meal").observe(.childAdded, with: { (snapshot) in | |
if let dictionary = snapshot.value as? [String : AnyObject]{ | |
let mealDetail = Meals() | |
mealDetail.setValuesForKeys(dictionary) | |
self.mealList.append(mealDetail) | |
//非同步load Tableview | |
DispatchQueue.main.async { | |
self.DishesTableView.reloadData() | |
} | |
} | |
}) | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return mealList.count //mealist是Meals.swift,也就是要在function外面 var mealList = [Meals](),詳細請下載github專案 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DishesTableViewCell | |
//這裡記得要跟Firebase中的五項一模一樣 | |
//cookName | |
cell.cookNameMeal.text = mealList[indexPath.row].cookName | |
//FoodName | |
cell.nameMeal.text = mealList[indexPath.row].FoodName | |
//cookTime | |
cell.timeMeal.text = mealList[indexPath.row].cookTime | |
//cookPic | |
if let profileImageUrl = mealList[indexPath.row].cookPic{ | |
let url = URL(string: profileImageUrl)//profileImageUrl是一個String(網址形式 http:fiebase.....) | |
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in | |
if error != nil{ | |
print(error) | |
return | |
} | |
//非同步load images | |
DispatchQueue.main.async { | |
cell.imageMeal.image = UIImage(data: data!) | |
} | |
}).resume() | |
} | |
//cookhow | |
cell.cookHowMeal.text = mealList[indexPath.row].cookhow | |
return cell | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment