我希望能夠在表格視圖單元格中的任意位置向左或向右滑動,以刪除帶有動畫的單元格而不顯示刪除按鈕。我怎樣才能做到這一點?向左或向右滑動UITableViewCell中的任意位置以刪除沒有刪除按鈕的單元格?
回答
我還沒有嘗試過,並且實施了這個,但我會給它一個鏡頭。首先,創建一個自定義的UITableViewCell,讓它有2個屬性,你可以使用
- 一個參考的tableView它正在被使用。
- 的indexPath找到TableView中的單元格。(注意,這需要每次刪除所有單元格時更新)
在您的cellForRowAtIndexPath:
,您創建自定義單元格,設置這些屬性。同時在單元格中添加一個UISwipeGestureRecognizer
cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)];
[cell addGestureRecognizer:swipeGestureRecognizer];
[swipeGestureRecognizer release];
確保手勢僅接收水平滑動。
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&&
((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft
||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES;
}
在你deleteCell:
-(void) deleteCell:(UIGestureRecognizer*)gestureRec
{
UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec;
CustomCell *cell=[swipeGestureRecognizer view];
UITableView *tableView=cell.tableView;
NSIndexPath *indexPath=cell.indexPath;
//you can now use these two to perform delete operation
}
不要忘記,首先初始化手勢識別器時,需要設置手勢識別器的方向以將其添加到單元格中。像這樣:swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris 2013-12-05 20:26:08
蘋果,該死的你不提供'(void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector'。 – 2014-03-10 17:24:59
因爲你必須在你的項目中添加這段代碼。
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you 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) {
//add code here for when you hit delete
}
}
否則鐵道部信息ü可以通過這個鏈接 enter link description here
Vivek2012和iWill - 感謝您的回覆,但我不想顯示DELETE按鈕,已經有在stackoverflow中的人的答案。 我希望細胞在接收到左手或右手滑動手勢的瞬間消失。請閱讀我的描述,謝謝 – carbonr 2012-01-30 06:54:52
實施
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
這兩種方法,如果UITableViewCellEditingStyle是UITableViewCellEditingStyleDelete,然後做某事與
抹去你的- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
就是這樣。
發表@MadhavanRP解決方案的工作,但它比它需要更加複雜。您可以採取更簡單的方法,並創建一個手勢識別器來處理表中發生的所有滑動,然後獲取滑動的位置以確定用戶滑動的單元格。
要設置手勢識別:
- (void)setUpLeftSwipe {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeLeft:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.tableView addGestureRecognizer:recognizer];
recognizer.delegate = self;
}
調用此方法viewDidLoad
要處理的刷卡:
- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer {
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
... do something with cell now that i have the indexpath, maybe save the world? ...
}
注:你的VC必須實現手勢識別委託。
- 1. 向右或向左滑動
- 2. 向UiTableViewCell添加一個刪除按鈕
- 3. unity3d v4.6中的動畫按鈕(向左滑動,向右滑動,向下滑動)
- 4. Swift:UITableView單元格中的按鈕被滑動刪除觸發
- 5. 單元格下移到右側單元格滑動刪除UITableView
- 6. 的iOS的UITableView - 顯示刪除按鈕在兩種向左滑動,然後單擊單元格
- 7. 刪除空單元格(向上移動)
- 8. 如何顯示左刪除按鈕UITableViewCell
- 9. Unslide刪除按鈕的UITableViewCell
- 10. UITableViewCell通過點擊左側的刪除控件直接刪除,但不顯示右側的刪除按鈕
- 11. 刪除UITableViewCell無刪除按鈕?
- 12. 按菜單按鈕從右向左滑動菜單
- 13. Android的ViewPager與ListViews有「向右滑動」刪除
- 14. 向左或向右滑動圖像
- 15. 全景項向左或向右滑動
- 16. 左滑動刪除和編輯,刪除
- 17. 向左或向右滑動並從左上角滑動更多
- 18. UITableview - 滑動刪除有時會導致單元格中的按鈕被點擊
- 19. 滑動即可刪除UITableViewCell
- 20. UITableViewCell的刪除按鈕動畫
- 21. Swipe刪除單元格而不必按下刪除按鈕Swift
- 22. jQuery:向左滑動並向右滑動
- 23. 想要在UITableView中顯示從右到左滑動的刪除按鈕
- 24. 擦除表格視圖單元格中的刪除按鈕
- 25. 從UITableViewCell中刪除單元格?
- 26. 滑動UITableViewCell刪除不UITableViewController中的commitEditingStyle
- 27. UITableViewCell,刪除按鈕框架?
- 28. 實現Facebook風格的滑動菜單從右向左滑動
- 29. 在UITableViewCell上向右滑動
- 30. 左右滑動隱藏的div,向右滑動活躍格
如果可以,發佈您的代碼? – 2012-01-30 06:34:54