【问题标题】:Is it possible to set the height of certain static UITableView cells and not all of them when conditions are met?是否可以在满足条件时设置某些静态 UITableView 单元格的高度而不是全部?
【发布时间】:2012-06-26 00:26:48
【问题描述】:

我有一个带有静态单元格的 UITableView,通过搜索,与搜索不匹配的单元格被隐藏。有没有办法设置隐藏单元格的行高。我曾尝试使用 if 语句,但它们最终会更改 tableview 中的所有单元格,而不是隐藏的单元格。

基本上我想知道当一个单元格被隐藏时,它的行高是否也可以改变

编辑 抱歉,当我发布问题时,我离开了我的代码

目前,我拥有带有静态单元格的 UITableView 和带有搜索显示控制器的搜索栏。 搜索栏的代码如下

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *cells;
@property(nonatomic) CGFloat rowHeight;




@implementation tableViewController

@synthesize searchBar;  
@synthesize cells;  
@synthesize rowHeight;  






- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
for(UITableViewCell *cell in self.cells) 
{
    UILabel *label = cell.textLabel;
    NSString *text = label.text;
   cell.hidden = searchText.length != 0 && ([text rangeOfString:searchText options:NSCaseInsensitiveSearch].location == NSNotFound);

    if (cell.hidden) {


        rowHeight = 10;

    }


       }

}

这是隐藏不匹配的单元格的原因。我遇到的问题是,当单元格被隐藏时,它们会占用显示器中的空间并节省空间并使事情看起来更好我希望将隐藏的单元格设置为 0 或 1 的高度以节省空间(10被用作一个例子,直到我可以让它工作)并让一切都对齐。 当搜索栏被取消或从中单击时,它将所有行高设置回 44。这段代码发生的情况是所有单元格都设置为 10,而不仅仅是隐藏的。

【问题讨论】:

  • 发布一些代码,以便我们了解您已经尝试过的内容。
  • 为什么要设置隐藏行的高度?
  • 我已经用我的代码更新了上面的帖子

标签: ios xcode uitableview static cells


【解决方案1】:

行高通过

控制
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

因此,当您隐藏一行时,无论您如何执行此操作,都可以重新加载该特定行。上面的方法可以检查单元格上的 BOOL 并使用它来确定返回的高度。

但是,还有另一种方法可以实现您想要的:

这是我如何过滤表格的示例。我的表格顶部有一个标准 UISearchBar,它使用搜索文本调用此方法:(一些方法调用是我项目的一部分……但你应该明白)

基本上,它给 tableview 一个不同的数据数组,然后重新加载 table。如果搜索被取消,或者 searchText 为空白 - 它将获得我原来的 tableData。

- (void) performSearchWithText:(NSString *)searchText {
    [super performSearchWithText:searchText];
    if ([searchText length] > 0) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title contains[cd] %@", searchText]; 
        NSArray *filteredArray = [[self.setListController sets] filteredArrayUsingPredicate:predicate];
        self.tableData = [NSArray arrayWithArray:filteredArray];
        [self.tableView reloadData];
    } else {
        self.tableData = [self.setListController sets];
        [self.tableView reloadData];
    }
}

这对我很有效。

编辑:或............

现在,如果您想继续按照您的方式进行操作.. 您可以试试这个....虽然看起来效率不是很高,但是您只有 32 个单元格,所以应该没问题。但理想情况下,您希望将数据和视图分开 - 因此您搜索数据并告诉视图进行更新。当前,您正在视图中搜索文本。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {    
    [self.hiddenCells removeAllIndexes];
    for(UITableViewCell *cell in self.cells) {
        UILabel *label = cell.textLabel;
        NSString *text = label.text;
        BOOL shouldHide = searchText.length != 0 && ([text rangeOfString:searchText options:NSCaseInsensitiveSearch].location == NSNotFound);
        if (shouldHide) {
            [self.hiddenCells addIndex:[self.tableView indexPathForCell:cell]];
       }         
    }
    [self.tableView reloadData];
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.hiddenCells containsIndex:indexPath) {
        return 0.0f;
    } else {
        return 40.0f;
    }
}

你还需要创建这个属性:@property (nonatomic, retain) NSMutableIndexSet *hiddenCells;

【讨论】:

  • 你在用这个静态单元格吗?
  • 不,但这有什么不同呢?您使用了多少个这些静态单元格?
  • 只是好奇,在项目中有 35 个静态单元格,我正在努力让您的代码适用于我的项目。你能显示更多信息吗?
  • 嘿,感谢您的帮助,我从代码中得到两个错误。两者都是“指向整数转换的不兼容指针,将'NSIndexPath *__strong'发送到'NSUInteger'(又名'unsigned int')类型的参数;”我还在学习目标 C,所以有时我可能会很愚蠢,但你真的帮助了我,再次感谢
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
相关资源
最近更新 更多