0

I am trying to insert a single custom image as the accessoryView for the cells of a UITableView. Yet I have a funny effect: at first the icon is inserted any two cells, then when I scroll up, all of the icons disappear and the image just shows on the bottom cell, to disappear as soon as the next one is shown scrolling. This is the code I am using:

NearCell *cell = [myTableView    dequeueReusableCellWithIdentifier:CellIdentifier];
nearContents* contents=[[self sourceForTableKind:tableView==self.myTableView favorites:NO] objectAtIndex:indexPath.row];
cell.bus.text=contents.bus;
cell.destination.text=contents.destination;
cell.selectionStyle=UITableViewCellAccessoryDetailDisclosureButton;
cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pinPoint"]];
return cell;

While in the UITableViewCell subclass nearCell I have:

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect accessoryFrame = self.accessoryView.frame;
    accessoryFrame.size.height = self.frame.size.height;
    accessoryFrame.size.width = self.frame.size.height;
    accessoryFrame.origin.y=self.frame.origin.y+21;
    accessoryFrame.origin.x=self.frame.origin.x+self.frame.size.width- accessoryFrame.size.width-5;
    self.accessoryView.frame = accessoryFrame;
}

I also tried to allocate the accessoryView lazily, thinking the problem could have been with the repeated allocations, but if I use the same image in all the cells the image is not shown altogether.

That is how the thing shows, the icon is just in the last cell : Table view with funny accessory view effect:

I also installed it in another tableView and there, for some reason, the layoutSubview of the subclass is not called and the accessoryViews regularly appears on all cells, albeit at a wrong size. Thus the problem seems resting in the layoutSubview callback, even if I ignore what it might be. The problem in both cases, though, is that the accessoryButtonTappedForRowWithIndexPath callback is not called upon touching the image, rather the didSelectRowAtIndexPath callback is invoked instead.

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75

2 Answers2

0

In fact when I dispensed of the layoutSubviews callback (by manually resizing the image), all the images are correctly shown even on the original table. The only persistent problem is that the accessoryButtonTappedForRowWithIndexPath is not called upon clicking the image.

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
0

I found this solution to also solve the accessory callback problem: Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate

I am actually managing the issue with a block in order to pass some internal parameters. This is the final solution:

        nearContents* contents=[[self sourceForTableKind:tableView==self.myTableView favorites:NO] objectAtIndex:indexPath.row];
        cell.bus.text=contents.bus;
        cell.destination.text=contents.destination;
        cell.selectionStyle=UITableViewCellAccessoryDetailDisclosureButton;
        cell.accessoryView=button;
        [self registerButton:button blockForKind:tableView==self.myTableView  favorites:NO];

-(void) registerButton:(UIButton*)button blockForKind:(BOOL)normal favorites:(BOOL)favorites{
    [button addEventHandler:^(id sender, id event) {
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:self.myTableView];
        NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint: currentTouchPosition];
        if (indexPath != nil)
        {
            [self tableView: self.myTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
        }
    }
forControlEvents:UIControlEventTouchUpInside];
}
Community
  • 1
  • 1
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75