En realidad es bastante fácil.
Simplemente crea una nueva subclase de UIControl y ponlo todo allí (no es necesario un controlador por separado). Llamémoslo ToggleImageControl.
@interface ToggleImageControl : UIControl
{
BOOL selected;
UIImageView *imageView;
UIImage *normalImage;
UIImage *selectedImage;
}
Cree un ToggleImageControl para cada celda y agréguelo en la posición adecuada.
ToggleImageControl *toggleControl = [[ToggleImageControl alloc] initWithFrame: <frame>];
toggleControl.tag = indexPath.row; // for reference in notifications.
[cell.contentView addSubview: toggleControl];
Agregue un UIImageView para que contenga la imagen. Agregue un objetivo para el evento táctil.
- (void) viewDidLoad
{
normalImage = [UIImage imageNamed: @"normal.png"];
selectedImage = [UIImage imageNamed: @"selected.png"];
imageView = [[UIImageView alloc] initWithImage: normalImage];
// set imageView frame
[self.view addSubview: imageView];
[self addTarget: self action: @selector(toggleImage) forControlEvents: UIControlEventTouchUpInside];
}
Establezca la propiedad de la imagen de UIImageView para actualizar la imagen; eso disparará el redibujado sin efectos secundarios.
- (void) toggleImage
{
selected = !selected;
imageView.image = (selected ? selectedImage : normalImage);
// Use NSNotification or other method to notify data model about state change.
// Notification example:
NSDictionary *dict = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: self.tag forKey: @"CellCheckToggled"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"CellCheckToggled" object: self userInfo: dict];
}
Obviamente tendrá que masajear algunas cosas. Probablemente quiera pasar los dos nombres de imagen para que sea más reutilizable, y también recomendaría especificar la cadena de nombre de notificación desde fuera del objeto también (suponiendo que esté utilizando el método de notificación)
hola rein, ¿cómo se puede hacer esta imagen para su pregunta? ¿Hay algún software disponible para esto? – raaz
¿Dónde obtiene el icono de la marca de verificación? –
¿También puedo encontrar el ícono de marca de verificación desde donde obtuve? – Ralphleon