[英]How to Implement Swipe Left to Right in UITableView Cell similar to iOS Mail
我找到了類似的解決方案,但僅適用於從右向左滑動的手勢。
SWTableViewCell擁有您可能需要的所有選項。
出列單元格時,只需根據需要設置左/右按鈕組。
cell.leftUtilityButtons = [self leftButtons];
cell.rightUtilityButtons = [self rightButtons];
cell.delegate = self;
通過將視圖控制器設置為其委托,您可以收聽按鈕點擊。 有關如何實施的完整詳細信息在該鏈接中
例 1:
例 2:
如果您正在尋找垂直堆疊的按鈕,請查看此。
我通常在表級別實現它。
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(leftSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(rightSwipe:)];
recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.tableView addGestureRecognizer:recognizer];
}
然后您就可以控制方向並可以隨意自定義
- (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
//do you left swipe stuff here.
}
- (void)rightSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
//do you right swipe stuff here. Something usually using theindexPath that you get that way
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
}
歸功於Jade Mind
從 iOS 11 開始,有本機規定(通過UITableViewDelegate
委托方法)在兩側的UITableView
的單元格上啟用“滑動操作”:
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? //For actions at the left
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? //For actions at the right
這些方法返回一個UISwipeActionsConfiguration
包含數組UIContextualAction
對於UIContextualAction
,您可以指定其標題、樣式、背景顏色、動作顏色,甚至是UIImage
並處理其回調(顯然 :-) )
這是一個示例實現:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let filterAction = UIContextualAction(style: .normal, title: "FILTER") { (action, view, bool) in
print("Swiped to filter")
}
filterAction.backgroundColor = UIColor.blue
return UISwipeActionsConfiguration(actions: [filterAction])
}
我知道這是一個老問題,但我發布這個只是為了幫助有人隨機經過......
你可以在 Swift 5 中這樣做:
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Closed")
success(true)
})
closeAction.image = UIImage(named: "tick")
closeAction.backgroundColor = .purple
return UISwipeActionsConfiguration(actions: [closeAction])
}
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Update", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
modifyAction.image = UIImage(named: "hammer")
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
}
您提供的鏈接中接受的答案適用於兩個滑動方向。
請注意,對於UISwipeGestureRecognizerDirectionLeft
和UISwipeGestureRecognizerDirectionRight
, gestureRecognizer.direction
返回YES
。
你只需要修改幾件事情:
更改在滑動時調用的選擇器,因此它會調用您的方法,而不是帖子示例中的方法,
並將滑動方向更改為僅從左到右,而不是像目前這樣的兩個方向,因為據我了解,您正在嘗試設置單向滑動。
所以你的代碼應該是這樣的:
// In cellForRowAtIndexPath:, where you create your custom cell
cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(YOUR_METHOD_GOES_HERE)];
[cell addGestureRecognizer:swipeGestureRecognizer];
.
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]] && ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
return YES;
}
請注意,您還可以使用已接受答案下方的答案,只需將手勢識別器direction
屬性修改為UISwipeGestureRecognizerDirectionRight
,而不是示例中的當前方向UISwipeGestureRecognizerDirectionLeft
。
如果您選擇實現這一點,您的 viewController 必須實現手勢識別器委托,您的代碼應如下所示:
// Call this method in viewDidLoad
- (void)setUpLeftSwipe {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeRightt:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableView addGestureRecognizer:recognizer];
recognizer.delegate = self;
}
- (void)swipeRight:(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? ...
}
注意 - 如果我沒有記錯的話,您需要自己創建單元格滑動動畫,因為我相信 Xcode 的默認單元格動畫僅在向左滑動時才出現。
您提供的鏈接MadhavanRP
和Julian
。 我只是修改了他們的答案,以更好地滿足您的需求。
不過,我自己還沒有嘗試過並實施過。
將自定義 TableViewCell 與滾動視圖一起使用,如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 120;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return header_view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array_field1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Schedule_cell";
Custom_Schedule_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (Custom_Schedule_Cell *)[Custom_Schedule_Cell cellFromNibNamed:@"Custom_Schedule_Cell"];
}
x = 0;
UILabel *lbl_title =[[UILabel alloc] initWithFrame:CGRectMake(x,0,cell.scroll_view.frame.size.width,cell.scroll_view.frame.size.height)];
lbl_title.text=@"Title Array";
[lbl_title setTextAlignment:NSTextAlignmentCenter];
[lbl_title setFont:[UIFont fontWithName:@"Raleway" size:18.0f]];
x += lbl_title.frame.size.width+10;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)];
[button setTitle:@"Button1" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor grayColor]];
[button addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchDown];
button.tag = indexPath.row;
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]];
x += button.frame.size.width+5;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)];
[button2 setTitle:@"Button2" forState:UIControlStateNormal];
[button2 setBackgroundColor:[UIColor grayColor]];
[button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchDown];
button2.tag = indexPath.row;
[button2.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button2.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]];
x += button2.frame.size.width+5;
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)];
[button3 setTitle:@"Button3" forState:UIControlStateNormal];
[button3 setBackgroundColor:[UIColor grayColor]];
[button3 addTarget:self action:@selector(button3:) forControlEvents:UIControlEventTouchDown];
button3.tag = indexPath.row;
[button3.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button3.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]];
[cell.scroll_view addSubview:lbl_title];
[cell.scroll_view addSubview:button];
[cell.scroll_view addSubview:button2];
[cell.scroll_view addSubview:button3];
x += button3.frame.size.width+5;
cell.scroll_view.contentSize = CGSizeMake(x,cell.scroll_view.frame.size.height);
cell.scroll_view.showsHorizontalScrollIndicator = NO;
cell.scroll_view.showsVerticalScrollIndicator = NO;
[cell.scroll_view setContentOffset:CGPointMake(0,0) animated:NO];
[cell.scroll_view setPagingEnabled:true];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Custom_Schedule_Cell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
// [selectedCell.scroll_view setContentOffset:CGPointMake(x2,0) animated:NO];
}
-(void)button1:(UIButton *)button
{
NSLog(@“button1 Click ");
[button setBackgroundColor:[UIColor redColor]];
}
-(void)button2:(UIButton *)button
{
NSLog(@“button2 Click");
[button setBackgroundColor:[UIColor greenColor]];
}
-(void)button3:(UIButton *)button
{
NSLog(@“button Click");
[button setBackgroundColor:[UIColor redColor]];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.