私は、UIViewController
TableViews デリゲートとデータソースプロトコルを実装する を持っています。「スワイプして削除」ジェスチャをセルに追加したいと思います。
どうすればいいですか。
メソッドの空の実装を指定しcommitEditingStyle
、Editing プロパティを YES に設定しました。
まだスワイプ機能は来ていません。
UISwipeGesture
各セルに個別に追加する必要がありますか?
または、何か不足していますか?
私は、UIViewController
TableViews デリゲートとデータソースプロトコルを実装する を持っています。「スワイプして削除」ジェスチャをセルに追加したいと思います。
どうすればいいですか。
メソッドの空の実装を指定しcommitEditingStyle
、Editing プロパティを YES に設定しました。
まだスワイプ機能は来ていません。
UISwipeGesture
各セルに個別に追加する必要がありますか?
または、何か不足していますか?
Danが上でコメントしたように、次のテーブル ビュー デリゲート メソッドを実装する必要があります。
tableView:canEditRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
注: iOS 6 および 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.");
}
editing:YES
セルスワイプで削除ボタンを表示する必要がある場合は、設定する必要はありません。tableView:canEditRowAtIndexPath:
編集/削除する必要がある行については、実装してそこから YES を返す必要があります。tableView の dataSource が UITableViewContoller のサブクラスである場合、これは必要ありません。このメソッドは、オーバーライドされていない場合、デフォルトで YES を返します。それ以外の場合はすべて、実装する必要があります。
編集:一緒に問題を発見しました -テーブルが編集モードでない場合にtableView:editingStyleForRowAtIndexPath:
返されます。UITableViewCellEditingStyleNone
// 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
}
}
クラスに以下を追加してみてください。
// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return(YES);
}
これは迅速なバージョンです
// 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
}
}
iOS 8.0 以降では、アクションをカスタマイズできます。
- (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];
}
}