我正在嘗試實現分組式樣的UITableView,比如Contact apps detailedView。我想最頂層的單元格是透明的,並在底部有一個UISegemtedControl。使用多個自定義的UITableViewCells
當我嘗試創建兩種不同類型的自定義單元格時,即使使用兩個不同的cellIdentifiers,也只加載第一個。
希望som指導。或者爲同一主題提供一些很好的教程提示。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/*
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
[backView release];
*/
static NSString *cellIdentifier1 = @"DetailCellStyle1";
static NSString *cellIdentifier2 = @"DetailCellStyle2";
if (indexPath.section == 0) {
// Load from nib
DetailCellViewController *cell = (DetailCellViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"DetailCellView"
owner:nil
options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DetailCellViewController *) currentObject;
break;
}
}
}
return cell;
}
else {
// Load from nib
DetailCellViewController2 *cell = (DetailCellViewController2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"DetailCellView"
owner:nil
options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DetailCellViewController2 *) currentObject;
break;
}
}
}
return cell;
}
return nil;
}
你的'numberOfSectionsInTableView:'方法返回什麼? – omz
@omz它返回2. – Silversnail
好吧,我假設你也許只有一個部分有兩行...再次查看你的代碼,我注意到你加載單元格1和單元格2的方式完全相同在「DetailCellView」筆尖中的「UITableViewCell」類型的第一個對象。因此,在這兩種情況下你都會得到同樣的細胞。 – omz