【问题标题】:Hide Null UItableView Cells/Rows隐藏 Null UItableView 单元格/行
【发布时间】:2012-12-28 11:37:08
【问题描述】:

我目前有一个 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? 看起来很相似,但没有解决我的问题。

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

非常感谢

托马斯

【问题讨论】:

  • 你把tableView的delegate设置为self了吗??
  • appdelegate.arrayDatabase 中有什么?你得到什么了吗?
  • 您的模型/数组中是否将空字符串作为@"" 或什么?
  • @sudha 如果我做 NSLog(@"%d",[appdelegate.arrayDatabase count]);是返回 30。相反,我只想计算数据库中的第 1 列。我认为这可以解决问题。有什么想法吗?

标签: ios uitableview


【解决方案1】:

添加此代码

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

有替代解决方案

你可以在numberOfRowsInSection函数中设置UITableView的高度,高度为[array count]*(cell height)

希望对你有帮助..

【讨论】:

  • 感谢您的快速回复。但它似乎没有解决问题。有什么想法吗?
【解决方案2】:

只需实现一个类似的 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 数组来填充您的表格视图

使用 [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 ];
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      假设您在从中填充 UITableView 的模型中获得 @""。

      您可以像这样删除该空白:

      [_arrays removeObjectIdenticalTo:@""];

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

      【讨论】:

        【解决方案5】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-19
          相关资源
          最近更新 更多