2012-12-28 118 views
1

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

可能有人请点我沿着正确的方向或提供答案:)

非常感谢

托马斯

+0

你是否将tableView的委托设置为自己? –

+0

appdelegate.arrayDatabase中有什么?你有东西吗? –

+0

你是否在@“”或者你的模型/数组中获得了空字符串? –

回答

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

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

非常感谢您指引我朝着正确的方向发展 – Tom

1

添加该代码

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

OR

有替代解决方案

可以设置的UITableView的高度numberOfRowsInSection功能,具有高度[数组数] *(单元格高度)

希望它可以帮助你..

+0

感谢您的快速响应。但它似乎解决了问题。有任何想法吗? – Tom

0

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

0

假设你正在从你填充UITableView的模型中获取@“”。

您可以删除空白像这样:

[_arrays removeObjectIdenticalTo:@ “”];

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

0

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

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

不使用页脚。但使其框架大小为零可能会导致应用程序崩溃,因此请使用高度为1的页脚。 这适用于我。 希望它有帮助。

相关问题