IOS

[ios/swift] UITableView indent가 작동하지 않을 때

hyukji 2023. 1. 27. 17:00

 

https://stackoverflow.com/questions/2502822/indentationlevelforrowatindexpath-not-indenting-custom-cell

 

indentationLevelForRowAtIndexPath not indenting custom cell

I have overridden the tableView:indentationLevelForRowAtIndexPath method in my UITableViewController derived class as follows: - (NSInteger)tableView:(UITableView *)tableView

stackoverflow.com

 

 

 

먼저는 TableView에서 editingMode일 때 Indenting이 정상적으로 작동하지 않았다. 따라서 shouldIndentWhileEditingRowAt , indentationLevelForRowAt 등 여러 함수들도 건드려 봤다. 동작하지 않았다 ㅎ

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    true
}

func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
    5
}

 

 

 

 

 

 

tableViewCell에서 layoutSubview에서 editingMode 일 때를 구분해 snapkit의 updateconstraints를 사용해 indent를 직접적으로 줬다. 하지만 자잘한 에러가 발생했고 그렇게 좋은 방법은 아니라고 생각됐다. 정 방법이 없을 때 최후의 수단 같은 느낌이다.

 

 

 

 

 

 

필자에게 도움이 되었던 것은 해당 글이었다. 

 

Have you added the subviews of your ProjectItemTableViewCell to the cell's contentView? Also, you need to set the subviews' autoresizing masks so that they are repositioned when the contentView size changes

 

[imgView, rightIconButton, stackView].forEach{
    addSubview($0)
}

 

필자는 다음과 같이 tableViewCell에다가 view들을 추가해 주었는 데 이렇게 주는 것이 아니라 contentView에 주어야 정상적으로 작동했다. 

 

[imgView, rightIconButton, stackView].forEach{
    contentView.addSubview($0)
}