iosobjective-cuitableviewaccessorytype

Keep UITableViewCell accessory on top


I have a UITableViewCell with a accessorytype detail indicator. When I resize the cell, the cells accessory centers itself horizontally. How can I make it stay on top, and not center itself?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];

    return cell;
}

I'd prefer doing it in the storyboard if possible, if not, I'm happy to hear how to do it programatically.


Solution

  • My mistake, Actually you can't do any change in default accessoryView of tableView, You need to create a custom accessoryView for your cell. Just ignore the accessory type because it will not longer matters once you create your own accessoryView.

    try this code in CellForRowAtIndexPath

    if(!cell.accessoryView)
    {
       UIImage *image = [UIImage imageNamed:@"image.png"];
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        button.frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal];
    
        //add target if necessary
        [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    
        //apply customisations 
        button.backgroundColor = [UIColor clearColor];
        cell.accessoryView = button;
    }