1

indexPathsForVisibleRows を介して NSIndexpaths の配列の intValue を読み取るにはどうすればよいですか?

ところで、if (cell == nil) 関数の前に、visibleCells と indexPathsForVisibleRows が機能しないのはなぜですか?

これが私のコードです:

cellForRowAtIndexPath メソッドでは:

    static NSString *identifierString;
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifierString];

    // when I use visibleCells and indexPathsForVisibleRows here, the app crashes

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierString] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;          
    }

    // when I use visibleCells and indexPathsForVisibleRows here, the app works

//cell implementation here

    return cell;
4

1 に答える 1

4

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathテーブルビューにセルが入力される場所です。可視セルが作成される前に参照しようとすると (if ステートメント内で実行されます)、アプリがクラッシュします。このallocコマンドは、作成するセルにメモリを割り当て、特定のパラメーターで初期化します。このメソッドは、 で指定した回数だけ呼び出されますnumberOfRowsInSection

すべてのセルを何度も再作成しないように、if ステートメントはセルが存在するかどうかをチェックし、それが nil の場合にのみ新しいセルを作成して代わりに作成します。

IndexPathの行の値を取得するには、そのint行プロパティを使用できます。例えば:

NSArray indexArray = [self.tableView indexPathsForVisibleRows];
int i=0;
while(i!=indexArray.count){
   //Log out the int value for the row
   NSLog(@"%d", indexArray[i].row);
   i++;
}

お役に立てれば

于 2012-07-16T13:24:25.180 に答える