0

私はまだ iPhone SDK に精通している途中です。

これは私がやろうとしていることです:

UIScrollView があり、各スクロール ビューには UITableView があり、カスタム UITableViewCell を実装しました。

望ましい機能は、最初は選択がなく、ユーザーが行を選択してスクロールし、次のスクロール ビューで別の選択を行って続行することです。ユーザーが後で選択を変更できるように、選択を維持したい。

ただし、私の場合、最初と2番目のUITableViewは問題なく、選択された行は選択されたままですが、3番目のUITableViewでは、最初のUITableViewと同じ行が「すでに」選択されています。選択したものを見たくありません。

私は自分が愚かなことをしていることを知っていますが、何を理解することはできません。どんな助けでも本当に感謝しています。

ありがとう、エイミー

関連するデータ ソースとデリゲート メソッドを次に示します。



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSLog(@"%s", __FUNCTION__);
 static NSString * ChoiceTableCellIdentifier = @"ChoiceTableCellIdentifier";
 choiceTableCell = (ChoiceTableCell *)[tableView dequeueReusableCellWithIdentifier:ChoiceTableCellIdentifier]; 
 if( choiceTableCell == nil )
 {
  choiceTableCell = [[[ChoiceTableCell alloc] 
       initWithFrame:CGRectZero
       reuseIdentifier:ChoiceTableCellIdentifier] autorelease];
 }
 return choiceTableCell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSLog(@"%s", __FUNCTION__);
 int newRow = [indexPath row];
 int oldRow = [lastIndexPath row];
 if ( (newRow != oldRow) || (newRow == 0) )
 {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
  UIImageView *indicatorN = (UIImageView *)[newCell.contentView viewWithTag:SELECTION_INDICATOR_TAG_1];
  indicatorN.image = [UIImage imageNamed:@"selected.png"];
  newCell.backgroundView.backgroundColor = [UIColor clearColor];

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
  UIImageView *indicatorO = (UIImageView *)[oldCell.contentView viewWithTag:SELECTION_INDICATOR_TAG_1];
  indicatorO.image = [UIImage imageNamed:@"notSelected.png"];
  oldCell.backgroundView.backgroundColor = [UIColor clearColor];
        lastIndexPath = indexPath;
 }
}



4

1 に答える 1

2

画像「selected.png」を持つセルを再利用しています。メソッド cellForRowAtIndexPath では、最後の選択に応じてセルを「選択」または「選択解除」する必要があります。つまり、indexPath が lastIndexPath と等しい場合は、背景を「selected.png」にする必要があり、そうでない場合は「noSelected.png」にする必要があります。セルを再利用すると、以前の状態が維持されるため、すべてを初期化する必要があります。

于 2009-09-29T23:31:08.473 に答える