class HomeTableViewCell: UITableViewCell {
static let identifier = "HomeTableViewCell"
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
class HomeCollectionViewCell: UICollectionViewCell {
static let identifier = "HomeCollectionViewCell"
@IBOutlet weak var imageView: UIView!
@IBOutlet weak var contentLabel: UILabel!
}
extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: HomeTableViewCell.identifier, for: indexPath) as? HomeTableViewCell else {
return UITableViewCell()
}
cell.categoryLabel.backgroundColor = .yellow
cell.collectionView.backgroundColor = .lightGray
cell.collectionView.tag = indexPath.row
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
if indexPath.row == 0 {
cell.collectionView.isPagingEnabled = true
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.row == 1 ? 300 : 170
}
}
extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomeCollectionViewCell.identifier, for: indexPath) as? HomeCollectionViewCell else {
return UICollectionViewCell()
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView.tag == 0 {
return CGSize(width: UIScreen.main.bounds.width, height: 100)
} else {
return CGSize(width: 150, height: 100)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if collectionView.tag == 0 {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
} else {
return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return collectionView.tag == 0 ? 0 : 10
}
}
구조체는 기본적으로 멤버와이즈 초기화 구문이 제공되기 때문에 추가적으로 초기화문을 작성하는 것이 필수는 아니다. 하지만 프로퍼티들 중 default값이 제공되어있는 초기화 구문 및 멤버와이즈 초기화 구문 모두를 사용하고 싶은 경우에는 extension을 사용하는 것이 좋다! (그렇지 않으면 다른 멤버와이즈 초기화구문을 직접 작성해서 준비해야하기 때문)
import Foundation
import UIKit
class User {
var name: String = ""
var age: Int = 0
}
let a = User() // 초기화 구문, 초기화 메서드 -> 기본적으로 생성된다고 해서 Default Initializer라고 불림
// let a = User.init()
struct UserStruct {
var name: String = ""
var age: Int = 0
}
let b = UserStruct(name: "name", age: 0) // -> Memberwise Initializer
let c = UserStruct() // 초기화가 이미 선언과 동시에 완료돼있다면 class처럼 default initalizer 사용할 수 있음
// 초기화 메서드를 사용할 수 있는 방법
// 아래 둘은 동일한 초기화 구문
let color = UIColor(red: 0.5, green: 0.5, blue: 1.0, alpha: 1)
let color2 = UIColor.init(red: 0.5, green: 0.5, blue: 1.0, alpha: 1)
class Coffee {
let shot: Int
let size: String
let menu: String
let mind: String
// Designated initializer(지정)
init(shot: Int, size: String, menu: String, mind: String) {
self.shot = shot
self.size = size
self.menu = menu
self.mind = mind
}
// 기본값 설정(2, tall)
// 여기서 다른 init 불러야돼서 미리 정의해놔야함
convenience init(value: String) {
self.init(shot: 2, size: "보통", menu: value, mind: "대략정성")
}
}
let coffee = Coffee(shot: 2, size: "tall", menu: "americano", mind: "듬뿍")
let coffee2 = Coffee(value: "latte")
값/255
과 같이 작성해줘야하는데 다음과 같이 편의생성자를 선언해주면 255라는 값을 생략하여 작성할 수 있어 한층 더 편리하다.extension UIColor {
convenience init(red: CGFloat, green: CGFloat, blue: CGFloat) {
self.init(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}
let customColor = UIColor(red: 20, green: 20, blue: 20)
navigationController?.navigationBar.prefersLargeTitles = true
https://developer.apple.com/forums/thread/131056
https://velog.io/@dlskawns96/UITableViewCell에-커스텀-Swipe-action-적용하기-Swift
https://furang-note.tistory.com/12
https://stackoverflow.com/questions/35106022/adding-buttons-to-toolbar-programmatically-in-swift