2016-08-07 12 views
0

私はストーリーボードの助けを借りずにUITableViewControllerを作成しています。UITableViewControllerをプログラムで実行するUITableViewCellスタイルを設定するにはどうすればよいですか?

私は、次のコードを持っている:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.cellReuseIdentifier = @"myViewControllerCellReuseIdentifier"; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellReuseIdentifier]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellReuseIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:self.cellReuseIdentifier]; 
    } 
} 

この場合、セルスタイルはありません。どうすれば修正できますか?

答えて

0

私はUITableViewを追加するだけで、あなたのビューコントローラをデリゲートとデータソースとして設定すれば十分だと思います。 cellForRowAtIndexPathにブレークポイントを設定すると、トリガされますか?

ないあなたがやっていることを確認してください、

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellReuseIdentifier]; 

むしろこれを行う、

self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 
self.tableView.delegate = self; 
self.tableView.dataSource = self; 
[self.view addSubview:self.tableView]; 

それはあなたが幸運、探しているものではありませんなら、私に教えてください。

+0

はい、すべてがトリガーされ、データが入力されます。私は 'UITableViewController'のクラス実装(上記)からオブジェクトを初期化(そしてプッシュ)するときに正しいスタイルを渡します。私は 'UITableView'の正しいスタイルを取得しますが、セルのスタイルを正しく取得できません(' UITableViewCell')。 –

+0

あなたのコードは、セルの初期化には大丈夫ですが、cellForRowAtIndexでセルを返しますか? – JingJingTao

関連する問題