0

私はこの本当に奇妙な問題を抱えています。

基本的に、いくつかのアウトレットを書き、Nib にリンクした UITableViewCell サブクラスがあります。

Cell.h のサブクラス

@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

Cell.m のサブクラス

@synthesize myImageView = _myImageView; // do i need this?


- (void) setUpCell... image:(UIImage*)image
{
       if(image)
        self.myImageView.image = image;
    else
        self.myImageView.image = nil;
} 

セルのテーブルビューを管理するView Controllerで、

次のコードを使用して Nib に登録しました。

[self.tableView registerNib:[UINib nibWithNibName:pathToNibForBaseMenuCell bundle:nil] forCellReuseIdentifier:identifierForBaseMenuCell];

cellForRowAtIndexPath では、セルをデキューして画像を設定します...

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell];

switch (indexPath.section) {


    case 2:
        [cell setUpCell...image:self.cogWheelImageForSettingsIcon];
        break;

    default:
        break;
}

return cell;

}

奇妙なことに、セクション 2 (上記のように) に 10 個のセルがあり、それらを同じ方法で設定したとします。一部のセルには歯車アイコンが表示され、一部のセルには表示されません! これらのセルをスクロールして表示または非表示にすると、アイコンを表示するセルと表示しないセルが変わるようです。

これは、再利用識別子の設定、nib ファイルでのサブクラスの設定など、セルの登録はすべて問題ないことを意味します (一般的な落とし穴)。

この動作パターンは、メモリに関連しているように思えます-何かがどこかで割り当て解除されているかのように...

これが私が何を意味するかを説明するための写真です:

ここに画像の説明を入力

助けてください!

ありがとう

VB

編集:

明確にするために、私はまだこのテーブルでデータを使用していませんが、それにもかかわらず、これらの歯車が表示されない理由はありません!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
switch (section) {
    case 0:
        return 1;
        break;

    case 1:
        return 10;
        break;

    case 2:
        return 10;
        break;

    default:
        return 0;
        break;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell];

switch (indexPath.section) {
    case 0:
        [cell setUpCell... text:@"Username" image:nil];
        break;

    case 1:
        [cell setUpCell... text:@"Magazine" image:nil];
        break;

    case 2:
    {
        NSString* str = [NSString stringWithFormat:@"Settings: %d",indexPath.row];
        [cell setUpCell... text:str image:self.cogWheelImageForSettingsIcon];
        break;
    }

    default:
        break;
}

return cell;
}

その出力は...

ここに画像の説明を入力

私のポイントは、2番目のセクションの「設定」にあるすべてのセルに同じ画像を設定している場合、なぜそれらの一部だけに画像が表示されるのですか?

カスタム セル クラス:

@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UILabel *mainLabel;
@property (weak, nonatomic) IBOutlet UILabel *arrowView;
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (void) setUpCelltext:(NSString*)text image:(UIImage*)image;


@implementation CustomCell

- (void) setUpCelltext:(NSString*)text image:(UIImage*)image
{
self.backgroundImageView.image = [UIImage imageNamed:pathForBackgroundImage];
self.myImageView.layer.cornerRadius = kCornerRadiusOfImageView;
self.myImageView.layer.masksToBounds = YES;

if(image)
    self.myImageView.image = image;
else
    self.myImageView.image = nil;

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:self.fontForText forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary];
self.mainLabel.attributedText = attrString;
}
@end

セルには、カスタム フォント、矢印ビュー、背景ビューがあります。これらはすべて正常に機能します。セクションによって変わります。うまくいかないイメージばかりです。cellForRowAtIndexPath によって呼び出されるこの関数は、提供されている場合は画像を設定し、提供されていない場合は imageview を nil に設定することがわかります。

4

2 に答える 2

0

UITableViewCellは、 を使用して古いセルから再描画されdequeueReusableCellWithIdentifier:ます。セルを再利用したくない場合は、tableView:cellForRowAtIndexPath:関数で毎回新しいセルを初期化する必要があります。

ただし、セルを再利用する場合 (推奨) は、画像が正しいセルのみに設定されるように、- (void) setUpCell... image:(UIImage*)image内部で毎回関数を呼び出す必要があります。tableView:cellForRowAtIndexPath:

サブクラス内に保存されているデータも使用しているようですUITableViewCellが、セルを別の行で再利用すると、元の行と同じセルであると見なされます。

例: 行 2 にセルが作成され、アイコン イメージがあります。セル 2 は画面から外れており、現在は行 16 に使用されています。行 16 には画像がないはずですが、セルは行 2 と同じセルであると認識し、画像を表示し続けます。

各セルを再利用し、それぞれに一意のデータを持たせたい場合はNSArray、セルを描画するために必要な情報を提供する外部データセット (例: ) のみを使用し、その情報を毎回セルに渡します。作成時にセルに与える情報に依存するのではなく、

于 2013-03-27T16:32:14.837 に答える