xcode 自定义 UITableViewCell 不使用 .xib(很可能是因为 init 方法存在缺陷)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15591364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:03:14  来源:igfitidea点击:

Custom UITableViewCell Not Using .xib (Most Likely Because of Flaw in init Method)

iphoneiosobjective-cxcodeuitableview

提问by sinθ

I subclassed the UITableViewCell in order to customize it, but I think I'm missing something because: 1) It's not working and 2) There are a couple of things I'm confused on. Along with customizing the look of the .xib file, I also changed the backgroundView, and that part is working fine. The part that I least understand/am most confused about is the init method, so I posted that here. If it turns out that is correct, please tell me so I can post more code that maybe the cause.

我对 UITableViewCell 进行了子类化以对其进行自定义,但我认为我遗漏了一些东西,因为:1) 它不起作用 2) 有几件事我很困惑。除了自定义 .xib 文件的外观外,我还更改了 backgroundView,这部分工作正常。我最不了解/最困惑的部分是 init 方法,所以我把它贴在这里。如果事实证明这是正确的,请告诉我,以便我可以发布更多可能是原因的代码。

This is the init method, which I customized.I'm sort of confused around the "style" idea and I think I'm just returning a normal UITableViewCell with a different backgroundView. I mean, there's nothing in there that refers to the .xib or does anything but change the .backgroundView from the self:

这是我自定义的 init 方法。我对“风格”的想法有点困惑,我想我只是返回一个具有不同背景视图的普通 UITableViewCell。我的意思是,那里没有任何内容引用 .xib 或做任何事情,只是从 self 更改 .backgroundView:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier wait: (float) wait fadeOut: (float) fadeOut fadeIn: (float) fadeIn playFor: (float) playFor
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CueLoadingView* lview = [[CueLoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 53)];
        self.backgroundView = lview;

        [self setWait:wait]; // in turn edits the lview through the backgrounView pointer
        [self setFadeOut:fadeOut];
        [self setFadeIn:fadeIn];
        [self setPlayFor:playFor];
    }
    return self;
}

Other than the .xib and several setters and getters, this is the only real part of my code, that relates to retrieving a cell.

除了 .xib 和几个 setter 和 getter,这是我代码中唯一与检索单元格相关的真实部分。

Additional Information:

附加信息:

1) This is my .xib, which is linked to the class. enter image description here

1) 这是我的 .xib,它与课程相关联。 在此处输入图片说明

2) This is the code that calls/creates the UITableView (the delegate/view controller):

2)这是调用/创建 UITableView(委托/视图控制器)的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CueTableCell";

    CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[CueTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier wait:5.0 fadeOut:1.0 fadeIn:1.0 playFor:10.0];
        [cell updateBarAt:15];
    }

    return cell;
}

回答by Martin R

The easiest way (available since iOS 5.0) to create a custom table view cell in a nib file is to use registerNib:forCellReuseIdentifier:in the table view controller. The big advantage is that dequeueReusableCellWithIdentifier:then automatically instantiates a cell from the nib file if necessary. You don't need the if (cell == nil) ...part anymore.

在 nib 文件中创建自定义表格视图单元格的最简单方法(自 iOS 5.0 起可用)是registerNib:forCellReuseIdentifier:在表格视图控制器中使用。最大的优点是,dequeueReusableCellWithIdentifier:如果需要,然后从 nib 文件中自动实例化一个单元格。你不再需要这个if (cell == nil) ...部分了。

In viewDidLoadof the table view controller you add

viewDidLoad您添加的表视图控制器中

[self.tableView registerNib:[UINib nibWithNibName:@"CueTableCell" bundle:nil] forCellReuseIdentifier:@"CueTableCell"];

and in cellForRowAtIndexPathyou just do

而在cellForRowAtIndexPath你只是做

CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CueTableCell"];
// setup cell
return cell;

Cells loaded from a nib file are instantiated using initWithCoder, you can override that in your subclass if necessary. For modifications to the UI elements, you should override awakeFromNib(don't forget to call "super").

从 nib 文件加载的单元格使用 实例化initWithCoder,如有必要,您可以在子类中覆盖它。对于 UI 元素的修改,您应该覆盖awakeFromNib(不要忘记调用“super”)。

回答by Mike D

You have to load the cell from the .xib instead:

您必须从 .xib 加载单元格:

if ( cell == nil ) {
    cell = [[NSBundle mainBundle] loadNibNamed:@"CellXIBName" owner:nil options:nil][0];
}

// set the cell's properties

回答by Dipen Panchasara

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CueTableCell";

    CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CueTableCell XibName" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [array objectAtIndex:0];
    }

    return cell;
}