2017-08-01 10 views
-3

次の実装があります。ここでは、customcellとしてフッタービューを追加しています。しかし、コンテンツがなければ、私はそれを表示したい場合は、最後のセルを表示したくありません。TableViewでカスタムセルを非表示にする

現在の実装では、常に最後のセルが表示されます。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [self.adminOrderElements count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return adminOrderElements[section].products.count + 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row < adminOrderElements[indexPath.section].products.count) 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
     if (!cell) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     NSArray * tempArray = adminOrderElements[indexPath.section].products; 
     cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:@"productname"]; 
     return cell; 
    } 
    else 
    { 
     static NSString *CellIdentifier = @"FooterCell"; 
     FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
      cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0) 
     { 
      cell.footerLabel.text = [NSString stringWithFormat:@"Notes: %@", adminOrderElements[indexPath.section].notes]; 
     } 
     else 
     { 
      cell.heightConstraints.constant = 1; 
      cell.footerLabel.text = @""; 
     } 
     return cell; 
    } 
} 

答えて

1

ありますがUITableViewデリゲートと非常にシンプル0

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(CHECK_IF_NO_CONTAIN) 
    return 0; 

return 40;// Normal height as you want 

} 
+0

カスタムセルを確認するにはどうすればよいですか? – hotspring

+0

このメソッドを実装するだけです。 – Nirmalsinh

+0

特定の配列インデックスのコンテンツを取得しない場合は、条件を設定する必要があります。その後、高さを0にできます。 – Nirmalsinh

0

そのにcellHeightを作ることができる一つの選択肢

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (dataObjects.count == 0){ 
     return 0; 
    } 
    return dataObjects.count; 
} 
1

私はあなたの場合のような他の句で条件を追加することができると思います特定の条件が満たされている場合は、このセルを他の賢者とは別のセルに表示します。

if (indexPath.row < adminOrderElements[indexPath.section].products.count) 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
     if (!cell) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     NSArray * tempArray = adminOrderElements[indexPath.section].products; 
     cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:@"productname"]; 
     return cell; 
    } 
    else //Here add your condition here if you have content for the section like I think this condition might work [adminOrderElements[indexPath.section].notes length]>0 
    { 
     static NSString *CellIdentifier = @"FooterCell"; 
     FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
      cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0) 
     { 
      cell.footerLabel.text = [NSString stringWithFormat:@"Notes: %@", adminOrderElements[indexPath.section].notes]; 
     } 
     else 
     { 
      cell.heightConstraints.constant = 1; 
      cell.footerLabel.text = @""; 
     } 
     return cell; 
    } 
} 
関連する問題