如何正確顯示到同一個UITableView中的不同UITableViewCell子類?如何在單個表中使用兩個不同的自定義UITableViewCell子類?
我正在使用這個問題的故事板。我試圖在單個表中使用兩個不同的UITableView單元的子類。 TTTextFieldCell
知道如何顯示文本框和TTPickerCell
知道如何向用戶展示UIPicker。在那裏,協議TTTableViewCell
指定單個選擇器:[setCellDetails (NSDictionary*) cellDetails]
每個UITableViewCell
子類都知道如何處理cellDetails字典來設置單元。
-(NSArray *)tableData {
if (!_tableData) {
_tableData =
@[ // Sections
@{ // Section Details
kSectionName:@"Scout Information"
,kRows:@[ // Rows
@{// Cell Details
kBoundProperty:self.scoutName
,kReuseIdentifier:kTextFieldCell
,kLabel:@"Name"
,kPlaceholderValue:@"Scout's Name"
}
,@{
kReuseIdentifier:@"TTPickerCell"
,kLabel:@"Type"
,kPlaceholderValue:@"Cadet, Junior, etc..."
}
,@{
kBoundProperty:self.yearsScouting
,kReuseIdentifier:kTextFieldCell
,kLabel:@"Years"
,kPlaceholderValue:@"Years of Experience"
,kKeyboardStyle:@(UIKeyboardTypeNumberPad)
}
]
}
,@{ // Section Details
kSectionName:@"Parent Information"
,kRows:@[
@{
kBoundProperty:self.parentName
,kReuseIdentifier:kTextFieldCell
,kLabel:@"Name"
,kPlaceholderValue:@"Parents's Name"
}
]
}
];
}
return _tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Line A
[self.tableView registerClass:[TTTextFieldCell class] forCellReuseIdentifier:@"TTTextFieldCell"];
// Line B
[self.tableView registerClass:[TTPickerCell class] forCellReuseIdentifier:@"TTPickerCell"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary* cellDetails = self.tableData[indexPath.section][kRows][indexPath.row];
NSString* reuseIdentifier = cellDetails[kReuseIdentifier];
// Line C
UITableViewCell<TTTableViewCell> *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
[cell setCellDetails:cellDetails];
return cell;
}
現在,這是問題。如果我註釋掉A行和B行,那麼TTTextFieldCells會正確顯示。但是C行將失敗,因爲它試圖用TTPickerCell對象調用TTTextFieldCell init,並且因爲TTPickerCell對象沒有相同的屬性而失敗。如果我取消對A行和B行的註釋,那麼單元格樣式默認爲UITableViewCellStyleDefault
,並且它們不顯示自定義控件。奇怪的是,如果我註釋掉A行,並取消註釋行B,那麼TTTextFieldCells
看起來不錯,但TTPickerCells
默認爲UITableViewCellStyleDefault
。
看來,如果我不註冊一個類,出列將返回第一個原型單元類的實例。如果我註冊了課程,那麼單元默認爲UITableViewCellStyleDefault
。
另外值得注意的是,initWithStyle:reuseIdentifier:選擇器只有在類被註冊時纔會被調用。
那麼,如何正確顯示到同一個UITableView中的不同UITableViewCell子類呢?我必須放棄使用故事板來設計單元格嗎?
預先感謝您。
謝謝@亞歷克斯。值得注意的是,這並沒有改變表達的行爲。 :(但它在viewDidLoad方法中註冊很有意義。感謝提示! –