我想爲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