0

絵コンテを使っています。セルの新しいクラスを作成します。ストーリーボードで、再利用識別子「userFullCell」を記述します。私のセルクラス.h

#import <UIKit/UIKit.h>

@interface UsefullLinksCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *shortTitle;

@end

.mデフォルト

#import "UsefullLinksCell.h"

@implementation UsefullLinksCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];

// Configure the view for the selected state
}

ストーリーボードでセルのUILabelとUIImageViewに接続されたプロパティ画像shortTitle

次に、UITableViewを使用するクラスで、「UseFullLinksCell.h」をインポートし、viewDidLoadで次のように記述します。

[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"];

tableViewは私のアウトレットです:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

次に、セルを作成しようとします。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"userFullCell";
  UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  NSDictionary *info = resourceArray[indexPath.row];
  if(info){
    cell.image.image = info[@"logo"];
    cell.shortTitle.text = info[@"title"];
  }
  return cell;
}

したがって、セルは初期化されますが、画像とテキストは割り当てられず、メソッドの終了後にセルに対して0x0000000を返します。このメソッドで標準セルコード用に作成する場合、つまりUITableViewCell * cellを使用すると、すべて問題ありません。どこで間違えることができますか?PS申し訳ありませんが、ストーリーボードからスクリーンショットを提供することはできません。自分のコンピューターではなく、必要に応じて画像を後で提供するためです。

4

3 に答える 3

6

クラスではなく、カスタムセルを作成したxibファイルを(registerNib:forCellReuseIdentifier :で)登録する必要があります。また、これが違いを生むかどうかはわかりませんが、これを置き換えてください:

UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

これで(メソッドの最後にあるforIndexPath:に注意してください):

UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

編集後:

ストーリーボードのテーブルビューにカスタムセルを追加し、それに識別子「userFullCell」を付けた場合、何も登録する必要はありません。実際、クラスを登録すると機能しなくなります。したがって、そのregisterステートメントをコメントアウトして、それが機能するかどうかを確認してください。

于 2013-02-14T07:34:51.563 に答える
1

あなたは何かを逃しました:

  NSArray *objects=[[NSBundle mainBundle]loadNibNamed:@"UsefullLinksCell" owner:self options:nil];
 UsefullLinksCell *cell =[objects objectAtIndex:0];

これにより、カスタムセルのオブジェクトが得られます。

于 2013-02-14T07:27:29.947 に答える
0

私はあなたが次のことをしなければならないと思います

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {
      // Initialization code

          image=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50.0,50.0)];
          shortTitle=[[UILabel alloc] initWithFrame:CGRectMake(60.0,0.0,200.0,50.0)];
     }
     return self;
}

それ以外の場合はすべて機能するはずです。重要なのは、コンポーネントがどこにも初期化されていないことです。xibからロードするか、手動で初期化する必要があります。

お役に立てれば。

于 2013-02-14T07:52:41.183 に答える