简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM