在我的 TableView 中,我尝试对大多数行使用默认的 TableView 单元格,但想对一行使用自定义单元格。问题是带有自定义单元格的一行。我显然没有正确加载它,因为我无法访问自定义单元格的属性。
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"KeepLoginCell" bundle:nil]
forCellReuseIdentifier:@"KeepLoginCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
if (indexPath.row == 0) {
cell.textLabel.text = @"Recent Purchases";
}
} else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
if (indexPath.row == 0) {
cell.textLabel.text = @"Payment Method";
}
} else if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"];
if (!cell) {
cell = [[KeepLoginCell alloc]init];
}
} else if (indexPath.row == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row];
}
}
return cell;
}
最佳答案
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Recent Purchases";
} else {
//TODO: handle this case
}
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Payment Method";
} else {
//TODO: handle this case
}
} else if (indexPath.section == 2) {
if (indexPath.row == 0) {
KeepLoginCell *customCell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"];
if (!customCell) {
customCell = [[KeepLoginCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KeepLoginCell"];
}
//TODO: set values to your custom cell
//e.g. customCell.myLabel.text = ...
cell = customCell;
} else if (indexPath.row == 1) {
cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row];
} else {
//TODO: handle this case
}
}
return cell;
}
关于ios - 加载多种类型的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33435997/