隐藏空UItableView单元格/行

时间:2012-12-28 11:37:09

标签: ios uitableview

我目前有一个uitableview。

数据来自sqlite文件。

sqlite文件的每一列都代表一个列表:标签,图像等

我用来获取行的代码是

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  AppDelegate* appdelegate = (AppDelegate*) [[UIApplication sharedApplication]delegate];
  return [appdelegate.arrayDatabase count];

}

填充单元格的代码

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *simpleTableIdentifier = @"simpleTableIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  if (cell == Nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
  }

  AppDelegate * appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
  ShowsList *sShows = [appdelegate.arrayDatabase objectAtIndex:indexPath.row];
  cell.textLabel.text = [sShows strName ];
}

我遇到的问题:

我想使用标签列填充ViewController1.tableview的单元格文本,但它返回空行并在视图中显示空行/单元格。

但是我想通过计算空单元格并将计数应用于numberOfRowsInSection:方法或简单地隐藏空单元格来隐藏行。

Hide empty cells in UITableView& How to hide a section in UITableView?看似类似的东西,但没有解决我的问题。

有人可以指出我正确的道路或提供答案:)

非常感谢

托马斯

5 个答案:

答案 0 :(得分:2)

添加此代码

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return [[UIView alloc]init];
    
}

OR

有替代解决方案

你可以在numberOfRowsInSection函数中设置UITableView的高度,高度为[数组计数] *(单元格高度)

希望它可以帮助你..

答案 1 :(得分:1)

只需实现类似

的reloadTableData方法
- (void)reloadTableData
{
// do a reload maybe from db and get yourobjects eg dbItems
// _tableItems = [NSMutableArray array];
    for (ShowsList *list in dbItems) {
        if ([list strName].length) {
            [_tableItems addObject:list];
        } 
    } 
}

然后只需使用_tableItems数组来填充你的tableview

使用[self reloadTableData]代替[self.tableview reloadData]

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  return _tableItems.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *simpleTableIdentifier = @"simpleTableIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  if (cell == Nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
  }

  ShowsList *sShows = [_tableItems objectAtIndex:indexPath.row];
  cell.textLabel.text = [sShows strName ];
}

答案 2 :(得分:0)

您应该创建数据对象的可变副本。首先迭代它。删除空/空条目。现在将此修改后的集合用于tableViewDelegate方法。

答案 3 :(得分:0)

假设您在填充UITableView的模型中获得了@“”。

你可以删除那个空白:

[_ arrays removeObjectIdenticalTo:@“”];

并使用此过滤后的数组作为模型来填充tableview。

答案 4 :(得分:0)

在viewDidLoad中将表格页脚设置为:

[yourTableView.tableFooterView setFrame:CGRectMake(0, 0, yourTableView.frame.size.width, 1)];

不使用页脚。但是将其框架设置为sizeZero可能会使应用程序崩溃,因此请使用高度为1的页脚。 这对我有用。 希望它有所帮助。