gpt4 book ai didi

ios - UITableView 自定义 accessoryView 只出现在最后一个单元格中

转载 作者:行者123 更新时间:2023-11-28 20:02:54 25 4
gpt4 key购买 nike

我正在尝试为 UITableView 使用自定义 accessoryView。作为 accessorView,我正在使用 UIImageView。我面临的问题是 accessoryView 只出现在 UITableView 的最后一行。

在 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;
}

这是截图:

enter image description here

最佳答案

一个 View 只能有一个父 View 。当您将自定义 View 添加到第二个单元格时,它将从第一个单元格中删除。因此,为每个表格 View 单元格创建自定义附件 View 。

- (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];
UIImageView *customAccessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)];
customAccessory.image = [UIImage imageNamed:@"icon_right"];
cell.accessoryView = customAccessory;
return cell;
}

关于ios - UITableView 自定义 accessoryView 只出现在最后一个单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com