首页 文章

如何在故事板中使用UISearchBar时填充自定义单元格

提问于
浏览
0

我正在使用故事板进行自定义单元格的tableview . 我在故事板中创建了自定义单元格而没有任何其他类 . 当我不使用UISearch栏时,一切正常,但是当我想为UISearchBarController tableview创建自定义单元格时,我不知道该怎么做 . 她是故事板中的自定义单元格,

custom cell

with cellIdentifier =“SelectionCell”

cell identifier

我没有使用任何自定义类 .

custom class

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SelectionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)  {
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [nibs objectAtIndex:0];
}

代码崩溃:'无法在bundle中加载NIB:'

我的自定义单元格没有NIB文件,我没有任何自定义UITableViewCell类来使用这样的alloc / init方法:

cell = [[myCustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

我怎样才能创建细胞?

2 回答

  • 0

    searchResultsTableView
    显示搜索结果的表视图 . (只读)

    @property(nonatomic, readonly) UITableView *searchResultsTableView
    

    搜索显示控制器管理搜索栏的显示,以及显示搜索结果的表格视图(原始的桌面视图样式) .

  • 0

    似乎没有自定义单元类就无法完成 . 因为我们没有故事板原型单元格来过滤表格视图,所以我们必须以编程方式创建单元格 . 为此,我们需要一个自定义单元类和.m文件中的initWithStyle:方法,如下所示 . 所以我创建了一个名为SelectionCell子类的UITableViewCell类,并在IB中将自定义单元类分配给SelectionCell类,并为我的自定义单元格中的所有UI控件创建IBOutlet到SelectionCell.h文件中

    @interface SelectionCell : UITableViewCell{
    
    }
    @property (strong, nonatomic) IBOutlet UIImageView *contactPictureID;
    @property (strong, nonatomic) IBOutlet UILabel *contactName;
    @property (strong, nonatomic) IBOutlet UILabel *emailAddress;
    @property (strong, nonatomic) IBOutlet UIImageView *selectedEmailState;
    
    @end
    

    在initWithStyle:方法中,我不得不以编程方式重新创建所有UI控件,并将它们作为子视图添加到contentView:

    // SelectionCell.m
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            self.selectionStyle = UITableViewCellSelectionStyleNone;
    
            _contactPictureID = [[UIImageView alloc] initWithFrame:(CGRectMake(10, 14, 48, 48))];
    
            _contactName = [[UILabel alloc] initWithFrame:(CGRectMake(66, 14, 201, 30))];
            _contactName.font = [UIFont boldSystemFontOfSize:20];
            _contactName.textAlignment = NSTextAlignmentLeft;
    
            _emailAddress = [[UILabel alloc] initWithFrame:(CGRectMake(66, 41, 201, 21))];
            _emailAddress.font = [UIFont systemFontOfSize:16];
            _emailAddress.textAlignment = NSTextAlignmentLeft;
    
            _selectedEmailState = [[UIImageView alloc] initWithFrame:(CGRectMake(275, 22, 32, 32))];
    
            [self.contentView addSubview:_contactPictureID];
            [self.contentView addSubview:_contactName];
            [self.contentView addSubview:_emailAddress];
            [self.contentView addSubview:_selectedEmailState];
        }
        return self;
    }
    

    现在我能够实例化我的自定义单元格 . 在cellForRowAtIndexPath方法中

    static NSString *CellIdentifier = @"SelectionCell";
    SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[SelectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
    

    而现在一切都在发挥作用 .

相关问题