티스토리 뷰
1. table view 만들고, 0 0 0 0 주기
2.
셀 하나 드래그해서 추가해주기
3.
이미지, 라벨 넣고 다들 간격 15씩 해쥼
이미지는 width, height 정해준다
왼쪽이랑 위쪽만 레이아웃 15씩 준다
라벨은 왼쪽, 오른쪽으로 15해주고 이미지랑 세로로 가운데 정렬 해줌
밑에 라벨은 위왼오아 다 15씩 해준다
4. cell ID값을 expandCell_ID로 해줌
5. !! 놀랍게도 VC 위쪽에 코드 작성해도 된다!!
6. 스토리보드 cell 클래스를 ExpandCell로 설정함.
7. 마우스 오른쪽 드래그 해서 ExpandCell 안에 라벨 연결해줌
8. table view의 data source랑 delegate 채택해줌
numberOfRowsInSection과 cellForRowAt구현은 필수!!
9. 스토리보드에서 data source랑 delegate 드래그해서 연결해주기
10. Model 만들어주기
11.
12.
13.
isExpand가 true이면 numberOfLines를 바꿔준다!
* 오토레이아웃이 의도한대로 되지 않는 현상이 발생한다면? *
- 1 : 전체 reload를 해본다
- 2 : heightForRowAt에서 정확한 높이를 넣는다
- 3 :
tableView.estimatedSectionHeaderHeight = 0
tableView.estimatedSectionFooterHeight = 0 이라고 해본다
- 4 : 애니메이션 효과를 없애기
UIView.setAnimationsEnabled(false)
tableView.reloadRows(at: [indexPath], with: .automatic)
UIView.setAnimationsEnabled(true)
(화면에 모든 애니메이션 효과를 없앰)
/// 전체 소스 ////
//
// ViewController.swift
// tableView
//
// Created by on 03/09/2019.
// Copyright © 2019 . All rights reserved.
//
import UIKit
class ExpandCell: UITableViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// Model
struct ExpandDataModel {
var description: String
var isExpand: Bool
}
var dataModels = [ExpandDataModel]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "expandCell_ID", for: indexPath) as! ExpandCell
cell.descriptionLabel.text = dataModels[indexPath.row].description
if dataModels[indexPath.row].isExpand == true {
cell.descriptionLabel.numberOfLines = 0
}
else {
cell.descriptionLabel.numberOfLines = 1
}
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dataModels[indexPath.row].isExpand = !dataModels[indexPath.row].isExpand
//해당되는 셀만 바꾸기
tableView.reloadRows(at: [indexPath], with: .automatic)
}
override func viewDidLoad() {
super.viewDidLoad()
let textArray = ["short Text",
"long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long ",
"long long long long long long long long long long long long long long long long long long "
]
for (_, value) in textArray.enumerated() {
dataModels.append(ExpandDataModel.init(description: value, isExpand: false))
}
}
}
'macOS, iOS' 카테고리의 다른 글
[iOS] int to string (0) | 2019.09.09 |
---|---|
[iOS] self-sizing table view cell 만들기 (0) | 2019.09.04 |
[iOS] DispatchQueue.main.async { (0) | 2019.09.02 |
[iOS] 앱이 비활성화 되는 경우 (홈버튼 눌렀을 때/ 전화/SMS시 ) 실행되는 코드 (0) | 2019.08.29 |
[iOS] UserNotifications 프레임워크 (0) | 2019.08.29 |