공부/Apple
[Swift] uitableview 선택 유지 해제(deselectRow)
초코팅촉
2024. 7. 24. 12:09
반응형
이번에는 UITableView와 관련된 코드를 알아보자.
우리가 테이블뷰를 초기에 생성하고 사용할때 터치를 하면 아래 사진처럼 선택된 상태가 유지된다.
이걸 그냥 한번씩 터치되고 바로 사라지게 만들고 싶으면
`tableView(_:didSelectRowAt:indexPath:)` 메서드 안에 아래의 코드 한줄만 넣으면 된다.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
그럼 아래 사진처럼 된다.
내용 추가)
아예 선택 안되게 막을수도 있다 그땐 아래처럼 쓰면 된다.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 이전 코드...
cell.selectionStyle = .none
return cell
}
반응형