ProgramingTip

UITableView, cellForRowAtIndexPath 동안 어떤 섹션을 어떻게 알 수 있습니까?

bestdevel 2020. 12. 7. 20:28
반응형

UITableView, cellForRowAtIndexPath 동안 어떤 섹션을 어떻게 알 수 있습니까?


도시 목록을 표시하는 UITableView가 있습니다. 국가별로 구분하고 싶습니다. 내 배열에서 올바른 항목을 선택하는 방법을 알아낼 수없는 것입니다. 섹션 1 (애리조나)에 2 개의 도시가 있고 섹션 2 (캘리포니아)에 2 개의 도시가있는 경우 cellForRowAtIndexPath, 섹션 2 중 City 1은 배열의 세 번째 항목 임에도 불구하고 어디에가 0입니다. 내 도시 배열을 각 항목에 도시 배열이있는 상태 배열로 전환하는 것에 대해 생각했지만 여전히 내가 어떤 섹션에 있는지 모르기 때문에 상태 배열 아래에서 어떤 도시 배열을할지 모르겠습니다. 액세스해야합니다.


메서드가 호출됩니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

indexpath 변수에는 행 및 섹션 속성이 포함됩니다.

indexPath.section  
indexPath.row

다음은 NSIndexPath 클래스에 대한 문서 링크입니다 .


이 사용할 수 당신있는 indexPath.sectionindexPath.row


Swift 3, 4 및 4.2

스위치 사용

switch indexPath.section {
        case 0:
            print("Write your code for section number one.")
        case 1:
            print("Write you code for section number two")
        default:
            return
        }

그렇지 않다면

if indexPath.section == 0 {
            print("Write your code for section number one.")
        } else if indexPath.section == 1 {
            print("Write your code for section number two.")
        }

참고 URL : https://stackoverflow.com/questions/1676394/uitableview-how-do-i-know-what-section-during-cellforrowatindexpath

반응형