我想显示自定义单元格,但我的代码一次只显示一个自定义表格单元格。我做错了什么?
我在 UIView 中设置了一个 UIViewController nib 和我的 UITableView。 nib中还有一个UITableViewCell,它的类是CustomCell(UITableViewCell的子类)。 UITableView 和 Cell 都是@sythesized IBOutlet @properties。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"CellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
if (cell == nil)
{
cell = standardCell; // standardCell is declared in the header and linked with IB
}
return cell;
}
最佳答案
您可以使用下面的示例代码;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"CellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
if (cell == nil)
{
NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in objectList) {
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell*)currentObject;
break;
}
}
}
return cell;
}
关于iphone - 自定义 UITableViewCell 只绘制一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4088412/