Rashad
Rashad

Reputation: 11197

UITableView custom accessoryView appearing only in the last cell

I am trying to use an custom accessoryView for UITableView. As accessorView I am using an UIImageView. The problem I am facing is the accessoryView is only appearing in the last row of UITableView.

In viewDidLoad I initialized the UIImageView.

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

In cellForRowAtIndexPath I am doing this:

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

This is the screenshot:

enter image description here

Upvotes: 0

Views: 542

Answers (3)

Apurv
Apurv

Reputation: 17186

A view can have only one superview. When you are adding custom view to second cell, it will be removed from first cell. So, create the custom accessory view for each table view cell.

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

Upvotes: 2

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

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

    ....
}

Upvotes: 1

Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

There is only one instance of customAccessory. You should create UIImageView for each cell.

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

Upvotes: 0

Related Questions