0

テーブルビュー tblLeaderboard を含むカスタム UIView サブクラス Leaderboard があります。インターフェイスはxibで作成されました。さらに、xib を持つ UITableViewCell サブクラス LeaderboardCell があります。テーブルビューにセルを登録するのに問題があります。

これが私が試したことです。最初にペン先をテーブルビューに登録しました:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        leaderInfo = [NSMutableArray array];
        tblLeaderboard.dataSource=self;
            [tblLeaderboard registerNib:[UINib nibWithNibName:@"LeaderboardCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"leaderboardCell"];

    }
    return self;
}

それから私が持っているセルを初期化するとき:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"leaderboardCell";

    LeaderboardCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
        cell = [[LeaderboardCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }

//ADDED THIS IN CASE DEFAULT CELL WAS LOADED
 cell.textLabel.text = [[leaderInfo objectAtIndex:indexPath.row] objectForKey:@"name"   ];
    cell.name.text = [[leaderInfo objectAtIndex:indexPath.row] objectForKey:@"name"];        
    cell.score.text = [[[leaderInfo objectAtIndex:indexPath.row] objectForKey:@"score"] stringValue];

    return cell;
} 

セルはペン先をロードしません。カスタムセルを作成するだけです(デフォルトのセルは名前とスコアを適切にロードするため、データソースが正常に機能していることがわかります)。

UITableView を制御するために ViewController ではなく UIView を使用することで、ここで問題が発生するかどうかわかりません。

4

1 に答える 1

0

私はxibsの経験はあまりありませんが、私の古いプロジェクトでは次のようにしました:

    static NSString *leaderBoardIdentifier = @"leaderboardCell"; //cell identifier same name as identifier in xib cell's attribute inspector
    LeaderboardCell *cell = (LeaderboardCell *)[tableView dequeueReusableCellWithIdentifier:leaderBoardIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:leaderBoardIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

h/m ファイルを含むセルの xib ファイルがあります。私の .h では、xib の要素を接続しました。

cell クラスの initWithStyle および setSelected メソッドには、デフォルトのコードが含まれています。

于 2013-10-01T15:54:24.317 に答える