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

时间:2014-04-21 05:05:17

标签: ios uitableview

我正在尝试为accessoryView使用自定义UITableView。正如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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
    }

    ....
}

答案 2 :(得分: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;
}