-2

nameLabel は機能しません。アプリケーションを実行すると、次のエラーが発生します: UITableViewCell nameLabel]: 認識されないセレクターがインスタンス 0x1fc4e090 に送信されました。しかし、nameLabel を textLabel として設定すると、機能します。

以下は私のコードです:

@interface ViewController ()
{
     NSMutableArray *books;
 }
@end

- (void)viewDidLoad
{

    Book *book1 = [Book new];
    book1.name = @"The adventures of tintin";
    book1.imageFile = @"tintin.jpg";


    Book *book2 = [Book new];
    book2.name = @"Avatar";
    book2.imageFile = @"avatar.jpg";
    books = [NSMutableArray arrayWithObjects:book1, book2, nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{<br>
    static NSString *simpleTableIdentifier = @"BookCell";

    UITableViewCell *cell = [tableV dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    //MyBookCell *cell = [tableV dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:simpleTableIdentifier];
    }
    //if (cell == nil) {
        cell = [[MyBookCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:simpleTableIdentifier];
    }
    Book *bk = [books objectAtIndex:indexPath.row];
    cell.nameLabel.text = bk.name; (customised label)

    return cell;
}

これは、カスタム テーブル セルのヘッダー ファイルです。

@interface MyBookCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@end
4

3 に答える 3

2

これは、UITableViewCell に nameLabel という名前のプロパティがないためです。textLabel.text の割り当ては正しいです または、カスタムセルクラスを実装でき、適切な名前のフィールドがあります 次に、代わりに

UITableViewCell *cell = [tableV dequeueReusableCellWithIdentifier:simpleTableIdentifier];

あなたは電話するべきです

MyCustomCell* cell = (MyCustomCell*)[tableV dequeueReusableCellWithIdentifier];
if(cell == nil){
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell"owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
于 2012-12-17T05:44:46.360 に答える
0

の nameLabel のようなプロパティはありませんUITableViewCell。をサブクラス化した場合はUITableViewCell、次のように独自のカスタム クラスに型キャストする必要があります。

YourClass *cell = (YourClass *)[tableV dequeueReusableCellWithIdentifier:simpleTableIdentifier];

また、次のように割り当てます。

cell = [[YourClass alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:simpleTableIdentifier];
于 2012-12-17T05:49:08.670 に答える
0

カスタム UITableViewCell をロードするには、次のスタイルを使用する必要があります。

static NSString *CellIdentifier = @"CellCustom";
CellCustom *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
cell.nameLabel.text = @"Text";
于 2012-12-17T05:50:13.907 に答える