0

カスタム セル クラスを作成せずにカスタム セルを作成します。私はこれが可能であることを知っています。ストーリーボードに、ラベル付きのプロトタイプ セルを持つ tableviewcontroller を作成しました。属性で、セル名「mycell」を設定しました。そして、私が使用したコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  static NSString *CellIdentifier = @"mycell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) 
   { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
   UILabel *title = (UILabel*) [cell viewWithTag:1];
   title.text = @"Hi";
   return cell;
}

しかし、アプリを実行すると、ラベルの付いたセルのない空のテーブルしか表示されません。私が使用したtableViewメソッドでは:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return 5; }

ありがとうございました。

4

1 に答える 1

0

UILabel をプログラムで追加する場合は、初期化を割り当てる必要があり、フレームを指定してサブビューとしてセルに追加する必要があります。

UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = @"Hi";
return cell;

する必要があります

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 50, 20)];
title.text = @"Hi";
[cell addSubview:title];
return cell;

ここに画像の説明を入力

于 2013-04-24T15:30:15.357 に答える