Skip to content

Instantly share code, notes, and snippets.

View KrauserHuang's full-sized avatar
:bowtie:
Learning iOS right now!

Krauser Huang KrauserHuang

:bowtie:
Learning iOS right now!
  • Taiwan
View GitHub Profile
override func viewDidLoad() {
super.viewDidLoad()
//cell and cell spacing
let itemSpace: CGFloat = 3
//how many colume in a row
let columeCount: CGFloat = 3
//將collectionViewLayout轉型成UICollectionViewFlowLayout(向下捲動)
let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout
//計算cell寬度,利用畫面的寬減去要的間距然後再除以要拆分成幾格(這樣可以避開不同的型號的iphone算出對應的寬度
let width = floor((collectionView.bounds.width-itemSpace*(columeCount-1))/columeCount)
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCollectionViewCell
// let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PropertyKeys.PhotoCollectionViewCell, for: indexPath) as! PhotoCollectionViewCell
// Configure the cell
//直接抓圖庫的圖片
// cell.photoImageView.image = UIImage(named: "photo\(indexPath.item)")
//藉由變數photos利用indexPath判斷要顯示哪個item
cell.photoImageView.image = UIImage(named: "\(photos[indexPath.item])")
return cell
}
@IBSegueAction func showDetail(_ coder: NSCoder) -> UIViewController? {
// guard let item = collectionView.indexPathsForSelectedItems else { return nil }
//利用indexPathsForSelectedItems?.first?.item(row)來對應要顯示的物件
guard let item = collectionView.indexPathsForSelectedItems?.first?.item else { return nil }
let controller = DetailViewController(coder: coder)
controller?.imageName = photos[item]
return controller
}
struct InstagramData: Codable {
let graphql: Graphql
struct Graphql: Codable {
//User information
let user: User
struct User: Codable {
let biography: String //簡介
// let external_url: URL //外部連結
let full_name: String //ID名稱
let profile_pic_url_hd: URL //大頭照
func fetchData() {
//使用URLSession來抓取需要的資料
let urlStr = "https://www.instagram.com/scottcbakken/?__a=1"
if let url = URL(string: urlStr) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
if let data = data,
let renderData = try? decoder.decode(InstagramData.self, from: data) {
self.igdata = renderData
//照片牆部分
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PropertyKeys.imageCell, for: indexPath) as! PostGridCollectionViewCell
//將圖片透過data抓下來變成image再回傳給cell
if let postImageURL = igposts[indexPath.item].node.thumbnail_src,
let url = URL(string: postImageURL) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
DispatchQueue.main.async {
cell.postGridImageView.image = UIImage(data: data)
//個人介紹、貼文數、被追蹤數、追蹤數
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let reusableCell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: PropertyKeys.header, for: indexPath) as! ProfileCollectionReusableView
reusableCell.profileImageView.layer.cornerRadius = reusableCell.profileImageView.frame.height / 2
if igdata != nil {
reusableCell.profileName.text = igdata.graphql.user.full_name
// reusableCell.postsNumLabel.text = "\(igdata.graphql.user.edge_owner_to_timeline_media.count)"
reusableCell.postsNumLabel.text = postsNumConvert(igdata.graphql.user.edge_owner_to_timeline_media.count)
// reusableCell.followersNumLabel.text = "\(igdata.graphql.user.edge_followed_by.count)"
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if ageTextField.text?.isEmpty == false,
heightTextField.text?.isEmpty == false,
weightTextField.text?.isEmpty == false,
activityButtonText.titleLabel?.text?.isEmpty == false {
return true
} else {
let controller = UIAlertController(title: "Please fill in all the blank", message: "You need to fill all the blank to calculate your BMR & TDEE", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
controller.addAction(okAction)
@IBSegueAction func showResult(_ coder: NSCoder) -> ResultViewController? {
let sex = sexSegmented.titleForSegment(at: sexSegmented.selectedSegmentIndex) ?? "Male"
let age = ageTextField.text!
let height = heightTextField.text!
let weight = weightTextField.text!
let activitySelection = activityButtonText.titleLabel?.text ?? "Sedentary(office job)"
let controller = ResultViewController(coder: coder)
controller?.bmrInfo = BmrInfo(sex: sex, age: age, height: height, weight: weight, activitySelection: activitySelection)
return controller
}
@IBAction func chooseAnswer(_ sender: UIButton) {
let selectedAnswer = sender.currentTitle!
// switch sender.tag {
// case 1:
// selectedAnswer = newQandA[questionIndex].optionA
// case 2:
// selectedAnswer = newQandA[questionIndex].optionB
// case 3:
// selectedAnswer = newQandA[questionIndex].optionC
// case 4: