簡體   English   中英

UITableView,我如何知道cellForRowAtIndexPath期間的哪個部分?

[英]UITableView, how do I know what Section during cellForRowAtIndexPath?

我有一個顯示城市列表的UITableView。 我想用國家把它們分開。 我似乎無法弄清楚如何從我的陣列中選擇正確的項目。 如果第1節(亞利桑那州)有2個城市而第2節(加利福尼亞州)有2個城市,則在cellForRowAtIndexPath第2節中,城市1的索引為0,即使它是我的數組中的第3個項目。 我想過把我的城市陣列變成一個狀態陣列,每個項目都有一個城市陣列,但我仍然不知道我在哪個部分,因此不知道狀態數組下的哪個城市數組我會需要訪問。

該方法被調用

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

indexpath參數包含row和section屬性。

indexPath.section  
indexPath.row

這是NSIndexPath類的文檔鏈接。

您可以使用indexPath.sectionindexPath.row

斯威夫特3,4和4.2

使用Switch

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 else

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.")
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM