0

UIViewController 内に UITableView を作成しようとしています。カスタム UITableViewCell もいくつか作成したいと思います。ただし、UILabel プロパティを UITableViewCell に追加する方法を理解するのに苦労しています。コードを実行すると、次のエラーが表示されます...「タイプ 'UITableViewCell' のオブジェクトにプロパティ '_label1' が見つかりません」

.h

@interface gideViewController : UIViewController <UITableViewDelegate,     UITableViewDataSource> {
}
@property (nonatomic, strong) IBOutlet UILabel *label1;
@property (nonatomic, strong) NSArray *data;

彼ら

@synthesize label1 = _label1;
@synthesize data = _data;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self._data count];
}


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

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell     alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}

//*********** This is where I get the compiler error ... "Property '_label1' not found on object of type 'UITableViewCell'"///
cell._label1.text = [self._data
                       objectAtIndex: [indexPath row]];

return cell;
}

- (void)viewDidLoad{
[super viewDidLoad];
self.data = [[NSArray alloc]
                 initWithObjects:@"ABC",
                 @"DEF",
                 @"XYZ",
                 @"JKY", nil];
}
4

1 に答える 1

1

カスタムテーブルビューセルをどこで作成しましたか?サブクラス化しましたか?サブコールを実行してサブクラスにMyTableViewCellという名前を付けた場合は、行を変更します

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

MyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

そして、ここで使用したいセル識別子でMyTableViewCellを登録します。とにかく、これらのタイプのセルには別の識別子を使用することをお勧めします。

static NSString *CellIdentifier = @"MyTableCell";

別のオプションはlabel1、作成時に各セルにサブビュー(プロパティではありません!!!)として追加することです。

if (cell == nil) {
    // Caution! If you want this line to be executed, then you MUST not register the cell identifier with any cell's class. Because if you register it then the dequeue method will automatically create an object of that class and return it. 
    // Plus you must not create this cell in IB using storyboard because then it is atuomatically registered. (Or do it properly and completely in IB. But then this code sniplet would be redundant) 
    cell = [[UITableViewCell     alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
    UILabel *label1 = [[UILabel alloc] init];  // I think you could use initWithFrame too for layouting purposes
    [cell.contentview addSubview:label1];
    lable1.tag = 99;   // this is to fetch the label on re-use
    // here you may layout the label, set colors etc.
}
// here you can fetch the label using the tag as identifier and then set its text value

もちろん、それを実装する方法は他にもあります。ただし、サブクラス化はおそらくより賢いオプションです。

于 2013-02-02T20:15:27.553 に答える