我正在尝试为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;
}

这是屏幕截图:

在此处输入图片说明

===============>>#1 票数:2

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

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

===============>>#2 票数:1

尝试这个,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ....
    if (cell == nil) {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil];
        cell = [cellArray objectAtIndex:0];
        UIImageView *customAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)];
        customAccessoryView.image = [UIImage imageNamed:@"icon_right"];
        cell.accessoryView = customAccessoryView;
    }

    ....
}

===============>>#3 票数:0

只有一个customAccessory实例。 您应该为每个单元格创建UIImageView。

- (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];

        customAccessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)];
        customAccessory.image = [UIImage imageNamed:@"icon_right"];
        cell.accessoryView = customAccessory;

    }

    // Here your code... 
    // An return cell
       return cell;
}

  ask by Rashad translate from so

未解决问题?本站智能推荐: