63

都市のリストを表示するUITableViewがあります。州ごとに分けたい。配列から適切なアイテムを選択する方法がわからないようです。セクション1(アリゾナ)に2つの都市があり、セクション2(カリフォルニア)に2つの都市がある場合、cellForRowAtIndexPath、セクション2の間に、都市1のインデックスは0になりますが、これは私の配列の3番目の項目です。CityArrayをStateArrayに変換することを考えました。ここで、各アイテムはCitiesの配列を保持しますが、自分がどのセクションにいるかわからないため、StatesArrayの下のどのCityArrayを使用するかわかりません。アクセスする必要があります。

4

3 に答える 3

131

メソッドが呼び出されます

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

indexpathパラメーターには、行とセクションのプロパティが含まれています。

indexPath.section  
indexPath.row

NSIndexPathクラスのドキュメントリンクは次のとおりです。

于 2009-11-04T20:35:42.247 に答える
10

あなたが使用することができindexPath.sectionますindexPath.row

于 2009-11-04T20:34:21.680 に答える
-1

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
        }

ifelseを使用する

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.")
        }
于 2019-06-13T13:21:32.230 に答える