2017-01-07 8 views
1

私は画像のようにダイナミックセルを作成しています。 テーブルビューでダイナミックセルを作成する方法を教えてください。私は単純なコードのデモを行っているどのようにテーブルビューセルでセルの内容に従って動的セルを作成できますか?

コード

self.arrayContainer = [[NSMutableArray alloc]initWithObjects: 
          [NSDictionary dictionaryWithObjectsAndKeys: 
          @"H.Rackham",@"name", 
          @"20/7/1994",@"date", 
          @"Lorem ipsum dolor sit amet, qui ex blandit posidonium efficiantur, sed te veri bonorum. Per id iriure utroque docendi. Stet patrioque incorrupte ea nam. Prima molestie accommodare nam in, inermis omnesque vix cu.       Est posse suavitate eu, ei justo labitur necessitatibus has, no sea agam liber. Deseruisse elaboraret eu ius. Est verterem sententiae an. Prompta elaboraret duo cu, aperiam dolorem has an, ubique nominati comprehensam no pri. In autem scripta concludaturque cum.",@"Description",nil], 
          [NSDictionary dictionaryWithObjectsAndKeys: 
          @"Sophia Rhodes",@"name", 
          @"20/7/1994",@"date", 
          @" Deseruisse elaboraret eu ius. Est verterem sententiae an. Prompta elaboraret duo cu, aperiam dolorem has an, ubique nominati comprehensam no pri. In autem scripta concludaturque cum.",@"Description", 
          nil], 
          nil]; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.arrayContainer count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *identifier = @"cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    cell.textLabel.text = [[self.arrayContainer objectAtIndex:indexPath.row ]valueForKey:@"name"]; 
    cell.detailTextLabel.text = [[self.arrayContainer objectAtIndex:indexPath.row ]valueForKey:@"date"]; 
    return cell; 

} 
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 44; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewAutomaticDimension; 
} 

画像

enter image description here

。 今このタイプの機能が必要です。

1)名前と日付は、画像に記載されているように表示が異なります。

2)コンテンツセルに応じてサイズを変更する必要があります。

あなたは

答えて

2

あなたはCustomCellを使用する必要がありますありがとうございました。

Objective-Cクラステンプレートを使用してプロジェクトに新しいファイルを追加します。それをDetailViewellと名前を付けて、UITableViewCellのサブクラスにします。あなたの最初の要件のための

これを従ってください、これはあなたを助ける: How to create Custom Cell

をあなたの第二の必要条件について:

あなたが設定する必要があり、その後標識するのに適切なautolayout制約を設定した場合これを実現するデリゲートメソッドに従うだけです。

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension;  
    return UITableViewAutomaticDimension; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; 
} 
+0

ありがとうございます。私にはお勧めします。どのようにラベルの自動レイアウト制約を設定できますか? – hd1344

+0

@ hd1344 http://stackoverflow.com/questions/12789013/ios-multi-line-uilabel-in-auto-layout –

+0

@ hd1344あなたの問題を解決しましたか? –

関連する問題