我很好奇使用 xib 文件来布局 UITableViewCell 内容的正确方法。当我尝试按照在 Internet 上找到的所有步骤进行操作时,我总是得到
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<NSObject 0x10fe7d790> setValue:forUndefinedKey:]: 此类与键 statusLabel 的键值编码不兼容。”
所以这是相关的代码
@interface MyCell : UITableViewCell
@property (nonatomic,strong) IBOutlet UILabel* messageLabel;
@property (nonatomic,strong) IBOutlet UILabel* statusLabel;
Run Code Online (Sandbox Code Playgroud)
在我的 UIViewController 中,我都尝试过
-(void)viewDidLoad {
[self.tableView registerNib:[UINib nibWithNibName:@"MyCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCellReuseID"];
}
Run Code Online (Sandbox Code Playgroud)
或使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCellReuseID";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( !cell ) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil]
lastObject];
// or sometimes owner is nil e.g.
//cell = …Run Code Online (Sandbox Code Playgroud) 我正在制作某种带有许多输入字段的"表单"(一些是文本字段,一些是开关,一些是从拾取器收集信息等).
这种形式有6个部分,某些答案的性质会影响界面的其余部分(例如,如果您选择拥有汽车,则会在下面显示更多选项).
为了实现这一点,我开始制作一个非常大的视图(mainView),UIScrollView它是a的子视图,但它变得太大,所以我决定为每个部分创建一个nib文件.我将每个nib文件的文件所有者设置为my MainFormViewController,然后为每个视图创建一个插座:section1View,section2View等.我加载了部分视图,-viewDidLoad如下所示:
// Section 1
UINib *nib1 = [UINib nibWithNibName:@"Section1" bundle:nil];
[nib1 instantiateWithOwner:self options:nil];
CGRect frame1 = self.section1View.frame;
frame1.origin.y = 10;
[self.section1View setFrame:frame1];
[self.mainView addSubview:self.section1View]; // mainView is the one I add to the scrollView
// Section 2 (goes 10px below section 1)
UINib *nib2 = [UINib nibWithNibName:@"Section2" bundle:nil];
[nib2 instantiateWithOwner:self options:nil];
CGRect frame2 = self.section2View.frame;
frame2.origin.y = frame1.origin.y + frame1.size.height + 10;
[self.section2View setFrame:frame2];
[self.mainView addSubview:self.section2View];
// Same approach for …Run Code Online (Sandbox Code Playgroud)