Comment activer le doigt cellule dans une TableView?
-
13-11-2019 - |
Question
J'ai un UIViewController
qui met en œuvre TableViews délégué et de la source de données les protocoles.Maintenant, je veux ajouter un "doigt" geste de cellules.
Comment dois-je aller à ce sujet.
J'ai donné un vide de mise en œuvre de commitEditingStyle
méthode et aussi de définir la Modification de la propriété de OUI.
Encore le mouvement de balayage de la fonctionnalité n'est pas venue .
Maintenant, ai-je besoin d'ajouter UISwipeGesture
pour chaque cellule ?
Ou ai-je raté quelque chose ?
La solution
Vous n'avez pas à définir editing:YES
si vous avez besoin de montrer bouton Supprimer sur la cellule de balayage.Vous avez à mettre en œuvre tableView:canEditRowAtIndexPath:
et retourner OUI partir de là, pour les lignes dont vous avez besoin pour modifier/supprimer.Ce n'est pas nécessaire lorsque votre tableView de la source de données est une sous-classe de UITableViewContoller - cette méthode, si elle n'est pas remplacée, retourne OUI par défaut.Dans tous les autres cas, vous devez la mettre en œuvre.
EDIT: Ensemble, nous avons trouvé le problème tableView:editingStyleForRowAtIndexPath:
retourné UITableViewCellEditingStyleNone
si le tableau n'était pas en mode d'édition.
Autres conseils
Comme Dan a commenté ci-dessus, vous avez besoin pour mettre en œuvre le tableau suivant afficher délégué méthodes:
tableView:canEditRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
Note:J'ai essayé ceci dans iOS 6 et iOS 7.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform the real delete action here. Note: you may need to check editing style
// if you do not perform delete only.
NSLog(@"Deleted row.");
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
S'il vous plaît essayez ce code swift,
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// let the controller to know that able to edit tableView's row
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
// add the action button you want to show when swiping on tableView's cell , in this case add the delete button.
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in
// Your delete code here.....
.........
.........
})
// You can set its properties like normal button
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction]
}
Essayez d'ajouter les éléments suivants à votre classe:
// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return(YES);
}
Conclusion de Kyr Dunenkoff le chat est
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
}
ne doit pas être défini si vous avez besoin de supprimer bouton apparaisse sur la geste de la main.
Si vous utilisez un NSFetchedResultsControllerDelegate
pour remplir le tableau en vue, cela a fonctionné pour moi:
- Assurez-vous que
tableView:canEditRowAtIndexPath
retourne true toujours Dans votre
tableView:commitEditingStyle:forRowAtIndexPath
la mise en œuvre, de ne pas supprimer la ligne directement à partir de l'affichage de la table.Au lieu de cela, le supprimer à l'aide de votre géré contexte de l'objet, par exemple:if editingStyle == UITableViewCellEditingStyle.Delete { let word = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Word self.managedObjectContext.deleteObject(word) self.saveManagedObjectContext() } func saveManagedObjectContext() { do { try self.managedObjectContext.save() } catch { let saveError = error as NSError print("\(saveError), \(saveError.userInfo)") } }
C'est le swift version
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Cela a été un problème pour moi aussi...je n'ai pu avoir de glisser pour supprimer le travail une fois tous les 10 ou si les tentatives.Il s'avère que la gesture
sur le TÉLÉVISEUR a été bloqué par un autre geste de la vue parent contrôleur.La TÉLÉVISION a été imbriquée dans une MMDrawerController
(balayer en mesure de tiroir de mise en page).
Juste de la configuration de la geste de reconnaissance dans le tiroir contrôleur de ne pas répondre à fermer les gestes dans l'accompagnement des tiroirs permis doigt pour travailler dans ma TV.
Vous pouvez également essayer de faire quelque chose comme ça avec la gesture delegate
:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Dans mon expérience, il semble que vous devez avoir editing
sur UITableView
mis à NO
pour le balayage de travail.
self.tableView.editing = NO;
Après iOS 8.0, vous pouvez vous en action
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil];
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row < count) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
}
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row < count) {
[posts removeObjectAtIndex:row];
}
}
Vous pouvez voir toutes les méthodes nécessaires par la création d'un UITableViewController classe (temporaire) dans XCode 5 et ensuite copier la méthode que vous souhaitez utiliser.Ces méthodes vous avez besoin seront commentés avec pré-remplis dans les lignes que vous désirez.