自定义UITableViewCell不使用.xib(最可能因为init方法中的缺陷) [英] Custom UITableViewCell Not Using .xib (Most Likely Because of Flaw in init Method)
问题描述
我将UITableViewCell子类化以便自定义它,但我认为我遗漏了一些东西,因为:1)它不起作用2)有一些我很困惑的事情。除了自定义.xib文件的外观之外,我还更改了backgroundView,这部分工作正常。我最不理解/最困惑的部分是init方法,所以我在这里发布了。如果事实证明这是正确的,请告诉我,以便我可以发布更多可能的代码。
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 may be the cause.
这是init方法,我定制的。我对风格的想法感到困惑,我想我只是返回一个带有不同backgroundView的普通UITableViewCell。我的意思是,那里没有任何东西引用.xib或做任何事情,只是从自己改变.backgroundView:
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:
- (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;
}
除了.xib和几个setter和getter之外,这是唯一真实的我的代码的一部分,与检索单元格有关。
Other than the .xib and several setters and getters, this is the only real part of my code, that relates to retrieving a cell.
附加信息:
1)这是我的.xib,链接到类。
1) This is my .xib, which is linked to the class.
2 )这是调用/创建UITableView(委托/视图控制器)的代码:
2) This is the code that calls/creates the UITableView (the delegate/view controller):
- (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;
}
推荐答案
最简单的方法(可用自iOS 5.0)在nib文件中创建自定义表视图单元格是在表视图控制器中使用 registerNib:forCellReuseIdentifier:
。最大的好处是 dequeueReusableCellWithIdentifier:
然后在必要时自动从nib文件中实例化一个单元格。你不再需要 if(cell == nil)...
part。
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.
In <$您添加的表视图控制器的c $ c> viewDidLoad
[self.tableView registerNib:[UINib nibWithNibName:@"CueTableCell" bundle:nil] forCellReuseIdentifier:@"CueTableCell"];
和 cellForRowAtIndexPath
你刚才
CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CueTableCell"];
// setup cell
return cell;
使用 initWithCoder $ c $实例化从nib文件加载的单元格c>,如有必要,可以在子类中覆盖它。要修改UI元素,您应该覆盖
awakeFromNib
(不要忘记调用super)。
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").
这篇关于自定义UITableViewCell不使用.xib(最可能因为init方法中的缺陷)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!