自作アプリを作ってみた -アプリ名:hanagasaki- article

はじめに

自作アプリを作りましたので、画面毎の機能とコードを説明したいと思います。
今回、article 画面の機能とコードを説明が記載されています。

使用環境

● OS:macOS Big Sur 11.3.1
● Xcode:12.5
● Swift:5.4
● DB Browser for SQLite:3.12.1

//  ArticleViewController.swift
import UIKit

class ArticleViewController: BaseViewController {
   
   @IBOutlet weak var articleTableView: UITableView!
   
   var modelArray = [modelArticle]()
   
   var id: String?
   var articleTitle: String?
   var url: String?
   
   override func viewDidLoad() {
       super.viewDidLoad()
       articleTableView.delegate = self
       articleTableView.dataSource = self
       addCustomCell()
       addHeaderBar("Article",
                    UIBarButtonItem(image: #imageLiteral(resourceName: "bars_24"), style: .done, target: self, action: #selector(tapHambugerMenu)),
                    UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(tapAddArticle)))
   }
   
   override func viewWillAppear(_ animated: Bool) {
       modelArray = ModelManager.getInstance().getAllArticle()
       articleTableView.reloadData()
   }
   
   func addCustomCell() {
       let customCell = UINib(nibName: "ArticleCell", bundle: nil)
       articleTableView.register(customCell, forCellReuseIdentifier: "ArticleCell")
   }
   
}

extension ArticleViewController : UITableViewDelegate, UITableViewDataSource {
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return modelArray.count
   }
   
   func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return 120
   }
   
   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let url = URL(string: modelArray[indexPath.row].url!)
       if UIApplication.shared.canOpenURL(url!) {
           UIApplication.shared.open(url!)
       }
   }
   
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = articleTableView.dequeueReusableCell(withIdentifier: "ArticleCell") as! ArticleCell
       cell.dateLabel.text = modelArray[indexPath.row].id
       cell.titleLabel.text = modelArray[indexPath.row].articleTitle
       return cell
   }
   
}
//  ArticleEntryViewController.swift
import UIKit

class ArticleEntryViewController: BaseViewController, UITextFieldDelegate {

   @IBOutlet weak var titleLabel: UILabel!
   @IBOutlet weak var urlLabel: UILabel!
   
   var articleTitle: String?
   var url: String?
   
   @IBOutlet weak var titleTextField: UITextField! {
       didSet {
           titleTextField.backgroundColor = .systemFill
           titleTextField.placeholder = "Add title ."
       }
   }
   
   @IBOutlet weak var urlTextField: UITextField! {
       didSet {
           urlTextField.backgroundColor = .systemFill
           urlTextField.placeholder = "Add url ."
       }
   }
   
   @IBOutlet weak var entryButton: UIButton! {
       didSet {
           entryButton.layer.cornerRadius = 10
       }
   }
   
   override func viewDidLoad() {
       super.viewDidLoad()
       addHeaderBar("ArticleEntry", nil, nil)
       titleTextField.delegate = self
       urlTextField.delegate = self
       tapEnable()
   }
   
   func tapEnable() {
       entryButton.isEnabled = false
       entryButton.backgroundColor = .black.withAlphaComponent(0.15)
   }
   
   @IBAction func TextFieldActionBtnInactive(_ sender: Any) {
       if titleTextField.text == "" {
           entryButton.isEnabled = false
           entryButton.backgroundColor = .black.withAlphaComponent(0.15)
       }
       else if urlTextField.text == "" {
           entryButton.isEnabled = false
           entryButton.backgroundColor = .black.withAlphaComponent(0.15)
       } else {
           entryButton.isEnabled = true
           entryButton.backgroundColor = UIColor(hex: 0xFF19FDD3)
       }
   }
   
   @IBAction func TextFieldActionBtnInactive1(_ sender: Any) {
       if urlTextField.text == "" {
           entryButton.isEnabled = false
           entryButton.backgroundColor = .black.withAlphaComponent(0.15)
       } else if titleTextField.text == "" {
           entryButton.isEnabled = false
           entryButton.backgroundColor = .black.withAlphaComponent(0.15)
       } else {
           entryButton.isEnabled = true
           entryButton.backgroundColor = UIColor(hex: 0xFF19FDD3)
       }
   }
   
   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       self.view.endEditing(true)
   }
   
   @IBAction func articleEntry(_ sender: UIButton) {
       
       let article = modelArticle(id: "", articleTitle: titleTextField.text, url: urlTextField.text)
       let isSave = ModelManager.getInstance().Save(article: article)
       
       dismiss(animated: true, completion: nil)
       print("isSave : -\(isSave)")
       
   }
   
   
}
//  ArticleCell.swift
import UIKit

class ArticleCell: UITableViewCell {
   @IBOutlet weak var dateLabel: UILabel!
   @IBOutlet weak var titleLabel: UILabel! 
   
   override func awakeFromNib() {
       super.awakeFromNib()
       // Initialization code
   }

   override func setSelected(_ selected: Bool, animated: Bool) {
       super.setSelected(selected, animated: animated)

       // Configure the view for the selected state
   }
   
}

おわりに

最後まで読んで下さりありがとうございます。
次回は、video画面の記事を投稿したいと思います。
この記事が何かの実装に参考になれば幸いです。

参考文献


いいなと思ったら応援しよう!