私はカスタムUITableViewCell
サブクラスを持っており、これが iOS 5 のカスタム セルをロードする正しい方法であると思われることを読みました。
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];
// Configure cell
cell.nameLabel.text = self.customClass.name;
}
return cell;
}
しかし、アプリを実行してもラベル テキストは表示されません。ただし、次の方法も試しました。
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)view;
}
}
// Configure cell
((CustomCell *)cell).nameLabel.text = self.customClass.name;
}
return cell;
}
このようにラベルが表示されますが、anyreuseIdentifier
はメソッドで設定されloadNibName:
ます。カスタムセルをロードする最良の方法は何ですか? iOS 5 以降をサポートする必要があります。initWithStyle:
テーブルビューのメソッドではなく、メソッドでセルのラベルとスタイルを構成する必要があるため、最初のアプローチが機能しない可能性がありますか?
ありがとう!