2

[indexPath 行] とスクロールについて他にも多くの質問があることは承知していますが、奇妙な数値が返される理由を理解できる人はいますか? セルを構成した方法の回避策を見つけられないようです。

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


    static NSString *CellIdentifier = @"ProfileIdentifier";

    NSUInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if(cell == nil){
        //My custom xib file is named "ProfileCell.xib"
        [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
                cell = profileCell;
        self.profileCell = nil;
    }
//name and info are both of type NSArray
    labelName.text = [name objectAtIndex:row];
    labelInfo.text = [info objectAtIndex:row];
    NSLog(@"Row: %d", row);

    return cell;
}

コンソールはロード時にこれを返します:

2012-08-26 09:09:10.966 Nav[8028:f803] 行: 0

2012-08-26 09:09:10.983 Nav[8028:f803] 行: 1

2012-08-26 09:09:10.986 Nav[8028:f803] 行: 2

2012-08-26 09:09:10.996 Nav[8028:f803] 行: 3

2012-08-26 09:09:10.999 Nav[8028:f803] 行: 4

2012-08-26 09:09:11.002 Nav[8028:f803] 行: 5

2012-08-26 09:09:11.004 Nav[8028:f803] 行: 6

2012-08-26 09:09:11.007 Nav[8028:f803] 行: 7

2012-08-26 09:09:11.010 Nav[8028:f803] 行: 8

2012-08-26 09:09:11.012 Nav[8028:f803] 行: 9

次に、画面外のセルまでスクロールすると、これが返されます。

2012-08-26 09:09:12.354 Nav[8028:f803] 行: 3

2012-08-26 09:09:12.404 Nav[8028:f803] 行: 2

2012-08-26 09:09:12.471 Nav[8028:f803] 行: 1

2012-08-26 09:09:12.622 Nav[8028:f803] 行: 0

ユーザーがスクロールするたびに、私の labelName.text と labelInfo.text が変化しています。
ただし、影響を受けるセルは一番下のセルだけです。9 番目のセルの labelName を [indexPath 行] と一致させるにはどうすればよいですか?

4

3 に答える 3

1

これは custom を扱う適切な方法ですUITableViewCell:


.hファイル内:

@interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UINib *profileCellNib;
}

// ...

@end

あなたの.mファイルで:

- (void)viewDidLoad {
    profileCellNib = [UINib nibWithNibName:@"ProfileCell" bundle:nil];
}

// ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProfileIdentifier";

    NSUInteger row = [indexPath row];
    UIProfileCell *cell = (UIProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) cell = [[UINib instantiateWithOwner:self options:nil] objectAtIndex:0];

    cell.labelName.text = [name objectAtIndex:row];
    cell.labelInfo.text = [info objectAtIndex:row];

    NSLog(@"Row: %d", row);

    return cell;
}

UIProfileCellは から継承されたクラスであると想定してUITableViewCellいます。カスタム セル ビューに別の名前を使用している可能性があります。その場合は-tableView:cellForRowAtIndexPath:、私の代わりにメソッドで正しい名前を設定する必要があります。

于 2012-08-26T09:53:20.210 に答える
1

キャッシング メカニズム全体が正しく設定されていません。

関連するコードは次のとおりです。

[[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
cell = profileCell; // profile cell is most likely nil here, you set cell to nil?
self.profileCell = nil; // if profile cell wasn't nil, it will surely be nil next pass.

初期化コードをログに記録する-tableView:cellForRowAtIndexPath:と、すべてのセルがキャッシュから返される代わりに再作成されることがわかります。あなたの状況では、初期化は、表示されている最初のセルに対して1回だけ発生するはずです。-dequeueReusableCellWithIdentifier:他のすべてのセルについては、メソッドを使用してキャッシュからデザインを取得する必要があります 。

于 2012-08-26T09:56:57.073 に答える
0

気にしないでください、私は回避策を考え出しました:

各ラベルにタグを設定しました。次に、コードを次のように編集します。

//labelName.text = [name objectAtIndex:row];
//labelInffo.text = [info objectAtIndex:row];

交換された:

UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [name objectAtIndex:row];
label = (UILabel *)[cell viewWithTag:2];
label.text = [info objectAtIndex:row];

これが、配列を介してラベルを変更することを決定した人に役立つことを願っています:x

編集:タグの設定が機能する理由はわかりませんが、元の方法ではラベルが変更されます。誰か説明できますか?

于 2012-08-26T09:28:48.473 に答える