【问题标题】:Loading UITableViewCell from xib results in loaded the nib but the view outlet was not set从 xib 加载 UITableViewCell 会导致加载 nib 但未设置视图出口
【发布时间】:2013-09-09 23:56:16
【问题描述】:

我正在尝试创建一个 UITableViewController,它的 UITableViewCell 是一个 xib 文件。 Xib文件为空(0个控件)所有控件都是这样以编程方式创建的

- (id) initWithTitle: (NSString*)p_title andImage: (UIImage*)p_image
{
    //adding checkbox & title
    MMCheckbox* myCheckbox = [[MMCheckbox alloc] initWithTitle:p_title andHeight:20];
    myCheckbox.frame = CGRectMake(self.frame.size.width - myCheckbox.frame.size.width -15,20, myCheckbox.frame.size.width, 20);
    myCheckbox.flat = YES;
    myCheckbox.strokeColor = [UIColor whiteColor];
    myCheckbox.checkColor = [UIColor magentaColor];
    myCheckbox.uncheckedColor = [UIColor clearColor];
    myCheckbox.tintColor = [UIColor clearColor];
    [self addSubview:myCheckbox];

    //adding the image
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake (15, 10, 20, 20)];
    [imageView setImage: p_image];

    return self;
}

在 xib 文件中,我放置了一个 UITableViewCell 并将它的类设置为我的类 MMCheckboxTableViewCell

在我的 tableViewController 中,我正在创建从 NIB 加载它的单元格,就像这样

- (UITableViewCell *)tableView:(UITableView *)p_tableView cellForRowAtIndexPath:(NSIndexPath *)p_indexPath
{
//create a UI tableViewCell
    UITableViewCell* cell = [p_tableView
                             dequeueReusableCellWithIdentifier:@"comboBoxCell"];
    if(cell == nil)
    {
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"MMComboBoxTableCell" bundle:nil];
        // Grab a pointer to the custom cell.
        cell = (MMComboBoxTableCell *)temporaryController.view;
    }
}

我查看了许多关于相同问题的帖子,并根据this 进行了检查以确保我所做的一切都是正确的。

请让我知道我缺少什么。

【问题讨论】:

    标签: objective-c uitableview xib


    【解决方案1】:

    我的问题在于我加载 XIB 的方式。

    通过更改以下内容:

    UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"MMComboBoxTableCell" bundle:nil];
    

    到这里:

    MMComboBoxTableCell* temporaryController = [[[NSBundle mainBundle] loadNibNamed:@"MMComboBoxTableCell" owner:self options:nil] objectAtIndex:0];
    

    问题解决了。

    【讨论】:

    • 你在这里做的很奇怪。首先,如果您在代码中制作所有控件,为什么要使用 xib?使用 xib 的目的是让您可以在 IB 中添加这些子视图。其次,即使您使用的是 xib,也不会使用视图控制器来加载它。您应该使用 registerNib:forCellReuseIdentifier:,您可以将其放入 viewDidLoad。 xib 应该只包含一个与视图控制器无关的独立表格视图单元格。
    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    相关资源
    最近更新 更多