【问题标题】:how to remove the empty title headers如何删除空的标题标题
【发布时间】:2015-04-28 15:52:43
【问题描述】:

我有一个表格视图,因为我有标题标题,它们运行良好,但是标题标题值的总和变为 null,我想在我的表格视图中隐藏那个 null 标题标题。我该怎么做这个过程。请帮我编码。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];

headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:@"Arial" size:16.0f];

headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}

我越来越像上图了,我不想隐藏空值标题。

【问题讨论】:

  • 你可以检查它是否为空 set tableView:heightForHeaderInSection: return 0 还有 tableView:viewForHeaderInSection: return [[UIView alloc] initWithFrame:CGRectZero]

标签: ios objective-c iphone uitableview ios7


【解决方案1】:

heightForHeaderInSection 中进行更改如果没有对象则返回 0

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   //Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
   NSArray *arrObjects = yourSectionsRowData[section];
   if([arrObjects count]>0])
      return 60.0;
   else
      return 0.0;
}

如果在viewForHeaderInSection 中找不到任何对象,也返回视图 nil

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   //Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
   NSArray *arrObjects = yourSectionsRowData[section];
   if([arrObjects count]>0])
      return nil;
   else
   {
      UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
      UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
      headerTitile.text = [reverseOrder1 objectAtIndex:section];

      headerTitile.textColor = [UIColor whiteColor];
      headerTitile.TextAlignment=NSTextAlignmentCenter;
      [headerView addSubview:headerTitile];
      headerTitile.font = [UIFont fontWithName:@"Arial" size:16.0f];

      headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
      return headerView;
   }
}

【讨论】:

  • yourTablViewDataSourceArray 是什么意思
  • 错误([__NSArrayM objectAtIndex:]:空数组的索引2超出范围')
  • 错误('NSInvalidArgumentException',原因:'-[__NSCFString count]: unrecognized selector sent to instance 0x8a88970')
  • 使用你的tableview数据源数组而不是节标题数组
猜你喜欢
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 2021-11-15
  • 1970-01-01
  • 2022-10-14
  • 2014-08-21
相关资源
最近更新 更多