오토 레이아웃으로 구성되어 있는 크기나 이런 부분이 테이블 뷰 셀을 만났을 때는 변형이 일어나 애니메이션 효과가 튀는 경우가 생김
오류 해결 방법
1. heightForRowAt에서 정확한 높이 지정으로 오류 해결
2. tableView.rerloadData()를 이용해 어느 정도 해결 가능
3. tableView.extimatedSelectionHeaderHeight = 0 / extimatedSelectionFooterHeight = 0으로 해결할 수도 있음
4. 애니메이션 효과를 없애는 방법
- 애니메이션 효과가 계속 없으면 안 되니까 reload가 끝나는 부분에 true로 바꿔줘야 함 안 그러면 모든 애니메이션 효과가 없어짐
UIView.setAnimationsEnabled(false)
import UIKit
// 클래스 생성
class ExpandCell: UITableViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
struct ExpandDataModel {
var description: String
var isExpand: Bool
}
// .init()과 ()는 같음 / []는 array 표시
var DataModels = [ExpandDataModel]()
override func viewDidLoad() {
super.viewDidLoad()
let textArray = ["short text",
"long long long long long long long text",
"short text",
"long long long long long long long text",
"short text",
"long long long long long long long text"]
// 안쓰는건 _로 표시
for (_,value) in textArray.enumerated() {
DataModels.append(ExpandDataModel.init(description: value, isExpand: false))
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataModels.count
}
// cellForRowAt - 셀을 구성하는 부분에 대한 개념
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 셀 선언
let cell = tableView.dequeueReusableCell(withIdentifier: "expandCell_ID", for: indexPath) as! ExpandCell
// 한 줄 한 줄 개념을 row로 보면 됨
cell.descriptionLabel.text = DataModels[indexPath.row].description
// expand가 true이면 확장
// expand이면 전체 줄 출력
if DataModels[indexPath.row].isExpand == true {
cell.descriptionLabel.numberOfLines = 0 // 0은 텍스트를 모두 표현
} else { // 확장 된 상태가 아니라면
// expand가 아니라면 1줄만 출력
cell.descriptionLabel.numberOfLines = 1
}
// 터치 했을때 나오는 효과를 없앰
cell.selectionStyle = .none
return cell
}
// 클릭했을때 사용되는 func / didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// expand 값을 바꿈
DataModels[indexPath.row].isExpand = !DataModels[indexPath.row].isExpand
// 화면에 모든 애니메이션 효과를 없앰
// reload가 끝나는 부분에서 true로 바꿔줘야 함
UIView.setAnimationsEnabled(false)
// table view 화면은 자동으로 갱신되지 않음
tableView.reloadRows(at: [indexPath], with: .none)
UIView.setAnimationsEnabled(true)
}
}
'Swift' 카테고리의 다른 글
Graph (0) | 2021.02.15 |
---|---|
Floating Button (0) | 2021.02.13 |
보안 키보드 만들기 (0) | 2021.02.13 |
StackView (0) | 2021.02.06 |
코드로 오브젝트 추가 (0) | 2021.02.06 |