0

iOS 6 でアプリを作成しています。これは、ViewController.m ファイルからのコードのスニペットです。

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

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        cell = _customCell;
        _customCell = nil;

    }

    cell.firstName.textLabel = @"dsdsds";
    cell.middleName.textLabel = @"N/A";
    cell.lastName.textLabel = @"daasdsdasa";

    return cell;
}

次のコード行でエラーが発生します ( Property 'firstName' not found on object of type 'CustomCell*'):

cell.firstName.textLabel = @"dsdsds";
        cell.middleName.textLabel = @"N/A";
        cell.lastName.textLabel = @"daasdsdasa";

CustomeCell.h:

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *firstName;

@property (strong, nonatomic) IBOutlet UILabel *middleName;
@property (strong, nonatomic) IBOutlet UILabel *lastName;

+(NSString*) reuseIdentifier;
@end

In the Outlets of CustomCell.xib:
firstName -> label
middleName -> label
lastName -> label

Referencing Outlets:
customCell -> File's Owner

Selecting the firstName label:
Referencing Outlets:
firstName -> CustomCell
firstName -> CustomCell -CustomCellIdentifier

Selecting the middleName label:
lastName -> Custom Cell - CustomCellIdentifier
middleName -> Custom Cell

Selecting the lastName label:
lastName -> Custom Cell
middleName -> Custom Cell- Custom Cell Identifier

それで、問題は何ですか?私の意見では、それはアウトレットと関係があります。

4

3 に答える 3

1

このエラー(Property 'firstName' not found on object of type 'CustomCell*'):は、コンパイラが。という名前のプロパティを認識していないことを意味しfirstNameます。通常はヘッダーファイルをインポートすることにより、クラス内で使用可能なプロパティをコンパイラに通知する必要があります。

したがって、テーブルビューコードがあるファイルの先頭に、次のように入力します。

#import "CustomCell.h"

@implementation(それはあなたのブロックの前にあり、他のブロックと一緒になっていることに注意してください#import

于 2013-01-30T04:43:41.713 に答える
1

私はあなたのコードにいくつかの間違いを見ました:

cell.firstName.textLabel = @"dsdsds";
cell.middleName.textLabel = @"N/A";
cell.lastName.textLabel = @"daasdsdasa";

問題は、UILabel の textLabel のようなプロパティがないことです。

次のように変更します。

cell.firstName.text  = @"dsdsds";
cell.middleName.text = @"N/A";
cell.lastName.text   = @"daasdsdasa";

プロパティを合成していない場合は、代わりに次を使用します。

cell._firstName.text  = @"dsdsds";
cell._middleName.text = @"N/A";
cell._lastName.text   = @"daasdsdasa";

また、次のように変更します。

if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
}

if (cell == nil) {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = (CustomCell *)[nib objectAtIndex:0];
}
于 2013-01-30T05:04:29.983 に答える
0

+reuseIdentifier.h ファイルにクラス メソッドがあるのはなぜですか? 継承元のUITableViewCellクラスにはその名前のプロパティがあるため、少し混乱します。

また、アウトレットには「弱い」を使用するのが慣習です@property (weak, nonatomic) IBOutlet UILabel *firstName;。これは、ビューがそのサブビューの強力なコピーを持つためです。

あなたの物件が見つからない理由がわかりません。実行時エラーです。ペン先に正しく配線されていない場合。そのビューをインスタンス化しようとするとすぐに例外が発生します。そのビューの「クラス」を CustomCell に設定しましたか? (ただし、そうしないと、通常の TableViewCell をデキューする必要があり、アウトレットを設定できなかったはずです...) うーん。

于 2013-01-30T05:00:31.163 に答える