【问题标题】:How to populate custom cell while using UISearchBar in storyboard如何在情节提要中使用 UISearchBar 时填充自定义单元格
【发布时间】:2014-02-05 14:01:42
【问题描述】:

我将故事板用于带有自定义单元格的表格视图。我在情节提要中创建了自定义单元格,而没有任何其他类。当我不使用 UISearch 栏时一切正常,但是当我想为 UISearchBarController tableview 创建自定义单元格时,我不知道该怎么做。 她是我在故事板中的自定义单元格,

with cellIdentifier = "SelectionCell"

而且我没有使用任何自定义类。

- (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];
}

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

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

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

如何创建单元格?

【问题讨论】:

    标签: ios uitableview storyboard uisearchbar uisearchdisplaycontroller


    【解决方案1】:

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

    @property(nonatomic, readonly) UITableView *searchResultsTableView
    

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

    【讨论】:

    • 我知道,问题是当 tableView == searchResultTableView 时,我必须创建一个自定义单元格来填充 searchResultTableView 单元格,但我没有 UITableViewCell 的自定义类。
    【解决方案2】:

    似乎没有自定义单元格类是无法完成的。因为我们没有故事板原型单元格来为过滤后的表格视图进行图形化操作,所以我们必须以编程方式创建单元格。为此,我们需要一个自定义单元类,并在 .m 文件中使用 initWithStyle: 方法,如下所示。 所以我创建了一个名为 UITableViewCell 的 SelectionCell 子类的类,并在 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]; }
    

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      相关资源
      最近更新 更多