Can a standard accessory view be in a different position within a UITableViewCell?

前端 未结 11 1050
谎友^
谎友^ 2020-12-05 09:57

I want my accessory to be in a slightly different place than normal. Is it possible? This code has no effect:

cell.accessoryType =  UITableViewCellAccessoryD         


        
11条回答
  •  伪装坚强ぢ
    2020-12-05 10:27

    Following the solution given by Ana I tried to better detect the accessory view, I look on the right side of the cell.

    Create a custom class that extends UITableViewCell and add this method:

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        if (self.accessoryType != UITableViewCellAccessoryNone) {
            float estimatedAccesoryX = MAX(self.textLabel.frame.origin.x + self.textLabel.frame.size.width, self.detailTextLabel.frame.origin.x + self.detailTextLabel.frame.size.width);
    
            for (UIView *subview in self.subviews) {
                if (subview != self.textLabel &&
                    subview != self.detailTextLabel &&
                    subview != self.backgroundView &&
                    subview != self.contentView &&
                    subview != self.selectedBackgroundView &&
                    subview != self.imageView &&
                    subview.frame.origin.x > estimatedAccesoryX) {
    
                    // This subview should be the accessory view, change its frame
                    frame = subview.frame;
                    frame.origin.x -= 10;
                    subview.frame = frame;
                    break;
                }
            }
        } 
    }
    

提交回复
热议问题