Objective c 目标C:UItableView注册表项B。怎么用?
我有Objective c 目标C:UItableView注册表项B。怎么用?,objective-c,uitableview,Objective C,Uitableview,我有UIViewController和它的内部UITableView。当我通过registerClass注册时,一切都正常,但忽略了自定义单元格中的内容:颜色、背景、子视图等(我正在子类化UITableViewCell)。当我通过registerNib注册时,它崩溃了,错误消息是“无法在bundle中加载NIB” 代码如下: - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:_pTableView]; [_pTa
UIViewController
和它的内部UITableView
。当我通过registerClass注册时,一切都正常,但忽略了自定义单元格中的内容:颜色、背景、子视图等(我正在子类化UITableViewCell
)。当我通过registerNib注册时,它崩溃了,错误消息是“无法在bundle中加载NIB”
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:_pTableView];
[_pTableView registerClass:[Element1Cell class] forCellReuseIdentifier:@"Element1CellID"];
// [_pTableView registerNib:[UINib nibWithNibName:@"Element1Cell" bundle:nil] forCellReuseIdentifier:@"Element1CellID"];
}
在-tableView:(UITableView*)tableView cellForRowAtIndexPath:(nsindepath*)indepath中,此:
oCell = [_pTableView dequeueReusableCellWithIdentifier:@"Element1CellID" forIndexPath:indexPath];
我错过了什么?我已经有一段时间没有在Objective-C上这么做了
(请只写Objective-C答案。无Swift。)如果您的自定义单元格在Nib/Xib中定义,则应使用注册表项nb
,但如果您只是自定义单元格的子类,则必须使用注册表项class
-(void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:_pTableView];
[_pTableView registerClass:[Element1Cell class]
forCellReuseIdentifier:@"Element1CellID"];
}
但在出列过程中,您必须强制转换返回的单元格(接口是一个基类),如
访问自定义类接口中声明的属性/方法。如果在Nib/Xib中定义了自定义单元格,则应使用registerNib
,但如果您只是自定义单元格的子类,则必须像以前一样使用registerClass
-(void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:_pTableView];
[_pTableView registerClass:[Element1Cell class]
forCellReuseIdentifier:@"Element1CellID"];
}
但在出列过程中,您必须强制转换返回的单元格(接口是一个基类),如
访问自定义类接口中声明的属性/方法。解决了问题的一部分-由于生命周期的原因,忽略了子视图。我已经添加了设置单元格的方法,现在一切都正常了。解决办法是:
- 在UITableViewCell中定义布局子视图中的设置
-(void) layoutSubviews {
[super layoutSubviews];
[self setBackgroundColor:__COLOR_BLUE];
}
- 在UItableViewCell中,定义将在创建对象后运行安装程序的方法
-(void) mSetup {
_pButtonAction = [[UIButton alloc] init];
[_pButtonAction setTitle:@"Element 1 Button" forState:UIControlStateNormal];
[_pButtonAction setTitleColor:__COLOR_BLACK forState:UIControlStateNormal];
[_pButtonAction setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_pButtonAction setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[_pButtonAction setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
[_pButtonAction setBackgroundColor:__COLOR_WHITE];
[_pButtonAction setTintColor:__COLOR_WHITE];
[_pButtonAction addTarget:self action:@selector(mButtonAction) forControlEvents:UIControlEventTouchUpInside];
[_pButtonAction setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:_pButtonAction];
[[_pButtonAction.topAnchor constraintEqualToAnchor:self.topAnchor] setActive:YES];
[[_pButtonAction.bottomAnchor constraintEqualToAnchor:self.bottomAnchor] setActive:YES];
[[_pButtonAction.leftAnchor constraintEqualToAnchor:self.leftAnchor] setActive:YES];
[[_pButtonAction.rightAnchor constraintEqualToAnchor:self.rightAnchor] setActive:YES];
}
- 在UITableView add中,在dequeueReusableCellWithIdentifier之后运行设置函数
id oCell;
...
oCell = (Element1Cell*)[_pTableView dequeueReusableCellWithIdentifier:@"Element1CellID" forIndexPath:indexPath];
oCell = [oCell mSetup];
...
return (UITableViewCell*)oCell;
问题的一部分已经解决-由于生命周期的原因,子视图被忽略。我已经添加了设置单元格的方法,现在一切都正常了。解决办法是:
- 在UITableViewCell中定义布局子视图中的设置
-(void) layoutSubviews {
[super layoutSubviews];
[self setBackgroundColor:__COLOR_BLUE];
}
- 在UItableViewCell中,定义将在创建对象后运行安装程序的方法
-(void) mSetup {
_pButtonAction = [[UIButton alloc] init];
[_pButtonAction setTitle:@"Element 1 Button" forState:UIControlStateNormal];
[_pButtonAction setTitleColor:__COLOR_BLACK forState:UIControlStateNormal];
[_pButtonAction setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_pButtonAction setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[_pButtonAction setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
[_pButtonAction setBackgroundColor:__COLOR_WHITE];
[_pButtonAction setTintColor:__COLOR_WHITE];
[_pButtonAction addTarget:self action:@selector(mButtonAction) forControlEvents:UIControlEventTouchUpInside];
[_pButtonAction setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:_pButtonAction];
[[_pButtonAction.topAnchor constraintEqualToAnchor:self.topAnchor] setActive:YES];
[[_pButtonAction.bottomAnchor constraintEqualToAnchor:self.bottomAnchor] setActive:YES];
[[_pButtonAction.leftAnchor constraintEqualToAnchor:self.leftAnchor] setActive:YES];
[[_pButtonAction.rightAnchor constraintEqualToAnchor:self.rightAnchor] setActive:YES];
}
- 在UITableView add中,在dequeueReusableCellWithIdentifier之后运行设置函数
id oCell;
...
oCell = (Element1Cell*)[_pTableView dequeueReusableCellWithIdentifier:@"Element1CellID" forIndexPath:indexPath];
oCell = [oCell mSetup];
...
return (UITableViewCell*)oCell;
您是否有单元xib?或者只是一个子类?如果你有xib,那么你需要使用注册表。在xib中添加了这个重用标识符吗?我没有xib,只是子分类你有单元xib吗?或者只是一个子类?如果你有xib,那么你需要使用注册表。在xib中添加了这个重用标识符吗?我没有xib,只是子类化了