[英]Returning 'self' while it is not set to the result of '[(super or self) init…]' when I initialize custom cell
In CustomCell.m I define init method where I want to load cell from the IB: 在CustomCell.m中,我定义了init方法,我想从IB加载单元格:
- (id)init {
self = [super init];
if (self) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
In the MyTableViewController.m in the method cellForRowAtIndexPath I initialize my custom cell 在方法cellForRowAtIndexPath中的MyTableViewController.m中,我初始化我的自定义单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell=[[CustomCell alloc]init];
return cell;
} }
Everything works as I expected but when I did Product -> Analyse
I get 一切都按照我的预期工作,但当我做
Product -> Analyse
我得到
Returning 'self' while it is not set to the result of '[(super or self) init...]'
What am I doing wrong? 我究竟做错了什么?
You are overwriting self
(returned from super init
) with the object returned from your array. 您正在使用从数组返回的对象覆盖
self
(从super init
返回)。 If you want to load a custom cell from a nib, do it in your cellForRowAtIndexPath method, or create a convenience class method on your custom cell that loads from the nib: 如果要从nib加载自定义单元格,请在cellForRowAtIndexPath方法中执行此操作,或者在从nib加载的自定义单元格上创建一个便捷类方法:
In your cellForRowAtIndexPath: 在你的cellForRowAtIndexPath中:
cell = [CustomCell cell];
In your cell's implementation: 在您的单元格的实现中:
+(CustomCell*)cell
{
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
return [nib objectAtIndex:0];
}
EDIT - changed method name since new* indicates that a retained object will be returned. 编辑 - 更改了方法名称,因为new *表示将返回保留的对象。
Keep your init method as below, and do the linking in the Interface Builder 保持您的init方法如下,并在Interface Builder中进行链接
- (id)init {
self = [super init];
if (self) {
}
return self;
}
And 和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
}
I met the same problem, I have fixed it by removing the code that like 我遇到了同样的问题,我通过删除喜欢的代码修复了它
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
return [nib objectAtIndex:0];
from the CustomView's definition init Method. 来自CustomView的定义init方法。
Put these code the at the place where you create the Custom. 将这些代码放在您创建自定义的位置。
What I am doing is 我在做什么
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code.
//
UITableViewCell *view = [[[NSBundle mainBundle] loadNibNamed:@"SmallCellView" owner:self options:nil] lastObject];
self.backgroundView = view;
}
return self;
}
then in the main class 然后在主类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SmallCellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SmallCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
For me this is working fine and in Product -> Analyse
is not giving any warning or error 对我来说,这工作正常,在
Product -> Analyse
中没有给出任何警告或错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.