Tengo un menuView en un controlador de vista de lista. El menuView agregado en UITableViewCell cuando se pega un botón más en la celda. Logré el efecto con singleton.
@implementation ProductsOperationMenu
static ProductsOperationMenu *_instance;
+ (instancetype)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] initWithFrame:CGRectZero];
});
return _instance;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
ZBMyProductsCell.m
@implementation ZBMyProductsCell
- (void)awakeFromNib
{
[super awakeFromNib];
_operationMenu = [[ProductsOperationMenu alloc] initWithFrame: CGRectZero];
}
- (IBAction)operationButtonClick:(UIButton *)sender {
if ([self.contentView.subviews containsObject:_operationMenu]) {
_operationMenu.hidden = ![_operationMenu isHidden];
} else{
[self.contentView addSubview:_operationMenu];
_operationMenu.hidden = NO;
}
[_operationMenu mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(205);
make.height.mas_equalTo(60);
make.bottom.mas_equalTo(self.operationButton).offset(0);
make.right.mas_equalTo(self.operationButton.mas_left).offset(-10);
}];
}
Sin Singleton, se convirtió en esto:
Entonces viene la pregunta.
Quiero poner el menuView en la vista del controlador, porque es único u oculto, que solía pertenecer a la celda.
¿Cómo convertir el diseño del botón más seleccionado a la vista del controlador? ¿Cómo usar los métodos para calcular?
- convertPoint:toView:
- convertPoint:fromView:
......
Lo hice de una manera simple. Aquí está el código:
- (void)clickOperationButtonOfProductsCell:(ZBMyProductsCell *)myProductsCell{
NSUInteger * operationIndex = [self.myProductsTableView.visibleCells indexOfObject:myProductsCell];
CGFloat originY = operationIndex.row * 110 + 50 + 40;
CGRect originFrame = CGRectMake(KScreenWidth - 55, originY, 0, 60);
CGRect finalFrame = CGRectMake(KScreenWidth - 260, originY, 205, 60);
self.operationMenuView.frame = originFrame;
[UIView animateWithDuration: 0.5 delay: 0 options: UIViewAnimationOptionCurveEaseIn animations:^{
self.operationMenuView.frame = finalFrame;
} completion:^(BOOL finished) { }];
}
¿Cómo lograrlo de manera más adaptativa?
3 respuestas
Quizás puedas intentarlo así:
// here is your cell where the more button belongs to
@interface ZBMyProductsCell: UITableViewCell
@property (nonatomic, copy) void(^moreAction)(ZBMyProductsCell *cell);
@end
@implementation ZBMyProductsCell
- (void)_moreButtonClicked:(UIButton *)sender {
!self.moreAction ?: self.moreAction(self);
}
@end
// here is your view controller where your table view belongs to
// this is a `UITableViewDataSource` delegate method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ZBMyProductsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZBMyProductsCell" forIndexPath:indexPath];
// Configure the cell...
cell.moreAction = ^(ZBMyProductsCell *cell) {
CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
// write your own code to show/hide the menu
};
return cell;
}
Cree una variable en cada modelo de celda llamada cellIndexpath y en cellForRow init
cell.cellIndexpath = indexpath
Eche un vistazo a UIPopoverPresnetationController
y vea si puede hacer el trabajo. Debería estar disponible en iPhone. Poner menu view
en controller’view
causará un problema al desplazar la vista de tabla .
Uso
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.detaiLabel.frame inView:self];
Nuevas preguntas
ios
iOS es el sistema operativo móvil que se ejecuta en el iPhone, iPod touch y iPad de Apple. Use esta etiqueta [ios] para preguntas relacionadas con la programación en la plataforma iOS. Use las etiquetas relacionadas [Objective-C] y [Swift] para problemas específicos de esos lenguajes de programación.