2017-08-01 66 views
-3

我有以下实现,我将footerview添加为自定义单元格。但是,如果没有内容,我不想显示最后一个单元格,如果有我想要显示它。在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

有一个选项,你可以做cellHeight 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

及其与UITableView代表很简单

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

我认为你可以在样,如果else子句添加条件某些条件满足,那么你想显示这个单元其他明智的不...

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; 
    } 
} 
相关问题