랭귀지/SWIFT
[Swift] 스위프트 사이드메뉴 테이블셀 선택시, 셀의 배경색깔을 바꾸고 싶을때
유키공
2018. 5. 7. 10:30
[Swift 3/4] Selecting and Deselecting UITableViewCells - Swift
//색상 변경을위해 cellForRowAt 메소드에 코딩
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell{
if self.selectedIndex == indexPath.row {
cell.firstView.backgroundColor = UIColor.orange // Highlight color
}else {
cell.firstView.backgroundColor = UIColor.clear // Original color
}
}
//배경 색상을 제거하려면 이전 행을 업데이트해야합니다.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
previousIndex = selectedIndex
if(selectedIndex == indexPath.row) {
selectedIndex = -1
} else {
selectedIndex = indexPath.row
}
let InPrev = IndexPath(row: previousIndex, section: indexPath.section)
let indexPathArray: [Any] = [InPrev, indexPath]
self.tableView.beginUpdates()
self.tableView.reloadRows(at:indexPathArray,with: UITableViewRowAnimation.automatic)
self.tableView.endUpdates()
}