我想为UITableView
使用自定义accessoryView
。至于accessorView
我正在使用UIImageView
。我面临的问题是accessoryView
只出现在最后一行UITableView
。UITableView自定义accessoryView只出现在最后一个单元格
In viewDidLoad我初始化了UIImageView。
customAccessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)];
customAccessory.image = [UIImage imageNamed:@"icon_right"];
在cellForRowAtIndexPath
我这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyDayCell *cell =(MyDayCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MyDayCell"];
if (cell == nil) {
NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil];
cell = [cellArray objectAtIndex:0];
}
switch (indexPath.row) {
case 0:
cell.image.image = [UIImage imageNamed:@"icon_guide"];
break;
case 1:
cell.image.image = [UIImage imageNamed:@"icon_media"];
break;
case 2:
cell.image.image = [UIImage imageNamed:@"icon_lives"];
break;
case 3:
cell.image.image = [UIImage imageNamed:@"icon_ask"];
break;
case 4:
cell.image.image = [UIImage imageNamed:@"icon_k"];
break;
default:
break;
}
cell.name.text = [dataSource objectAtIndex:indexPath.row];
cell.accessoryView = customAccessory;
return cell;
}
这是截图:
您在所有单元格中使用了相同的对象'customAccessory',因此它将显示在最后一个单元格中。为每个单元创建附件视图将解决该问题。 – Akhilrajtr
是的。但为什么会发生?为什么我不能为所有单元使用相同的accessoryView? – Rashad
视图只能有一个父级。无法同时多次显示单个视图实例。 – rmaddy