解決iOS 加在UIScrollView上的UITableView滑動手勢衝突問題辦法

來源:互聯網
上載者:User

在UITableView裡面實現cell的左滑刪除功能是挺簡單的,一般大家都會做。但是,如果把UITableView加在UIScrollView上的時候,就會產生一系列的問題。
首先闡明是因為UITableView列表太寬,超出了螢幕的寬度,所以只好加在UIScrollView上,控制UIScrollView的contentSize實現列表的左右滑動。

一般我們的使用者體驗都是希望表格是緊貼螢幕邊框,不讓使用者看到螢幕多餘出來的部分,這時候就要把UIScrollView的屬性bounces給關閉,設定為NO,也就是所謂的彈簧效果。
下面我們來說說把UITableView加在UIScrollView上的時候,產生的問題:

有些項目要求給UITableView做左滑刪除功能,但是這個UITableView列表是加在UIScrollView上的,這時候如果你向左滑動,你會發現沒法看到左滑出來的刪除按鈕,但是也有時候會出來,我想可能是這時候的手勢滑動響應在了UITableView身上,這就是滑動手勢衝突的問題。經過一番查閱資料,終於找到瞭解決辦法,辦法就是重寫UIScrollView類,繼承UIScrollView。
MyScrollview.h檔案

#import <UIKit/UIKit.h>
 
@interface MyScrollview : UIScrollView<UIGestureRecognizerDelegate>
 
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
 
@end
MyScrollview.m檔案


#import "MyScrollview.h"
 
@implementation MyScrollview
 
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (gestureRecognizer.state != 0) {
        return YES;
    } else {
        return NO;
    }
}
 
@end
接下來就可以在主介面ViewController裡匯入#import “MyScrollview.h”,使用MyScrollview代替UIScrollView。
附上我寫的一個小demo代碼:

#import "ViewController.h"
#import "sideslipTableViewCell.h"
#import "MyScrollview.h"
 
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>{
    UITableView *sideslipTableView;
    NSMutableArray *dataArray;
    MyScrollview *mainScrollView;
    
}
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    dataArray = [NSMutableArray arrayWithArray: @[@"1111",@"2222",@"3333",@"4444",@"5555",@"6666",@"7777",@"8888",@"9999"]];
}
#pragma mark - 初始化UI
- (void)initUI{
    self.view.backgroundColor = RGB(242, 242, 247);
    self.automaticallyAdjustsScrollViewInsets = NO;
    mainScrollView = [[MyScrollview alloc] initWithFrame:CGRectMake(0, 44 + 10, kScreenWidth, kScreenHeight - 44 - 10)];
    mainScrollView.bounces = NO;
    mainScrollView.delegate = self;
    [mainScrollView setDelaysContentTouches:NO];
    [mainScrollView setCanCancelContentTouches:NO];
    [self.view addSubview:mainScrollView];
    
    sideslipTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, 440, kScreenHeight - 60) style:UITableViewStylePlain];
    sideslipTableView.backgroundColor = [UIColor clearColor];
    sideslipTableView.delegate = self;
    sideslipTableView.dataSource = self;
    sideslipTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [mainScrollView addSubview:sideslipTableView];
    
    mainScrollView.contentSize = CGSizeMake(440, 0);
}
 
#pragma mark - 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}
 
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 46;
}
 
#pragma mark - cell內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indefier = @"cell";
    sideslipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];
    if (!cell) {
        cell = [[sideslipTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lable.text = dataArray[indexPath.row];
    return cell;
}
 
//先要設Cell可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
 
//定義編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
 
//進入編輯模式,按下出現的編輯按鈕後,進行刪除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [dataArray removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source.
        [sideslipTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
 
//修改編輯按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end
這裡實現的是左滑刪除的功能,如果你需要實現左滑出現多個按鈕,如:刪除和關閉,將上面的方法- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 注釋掉,然後實現下面的方法:

 

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定義
        NSLog(@"點擊刪除");
    }];
    deleteRoWAction.backgroundColor = RGB(210, 207, 209);
    //此處是iOS8.0以後蘋果最新推出的api,UITableViewRowAction,Style是划出的標籤顏色等狀態的定義,這裡也可自行定義
    UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關閉" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"點擊關閉");
    }];
    editRowAction.backgroundColor = [UIColor redColor];//可以定義RowAction的顏色
    return @[editRowAction,deleteRoWAction];//最後返回這倆個RowAction 的數組
}

注意:下面這兩個屬性必須加上:


[mainScrollView setDelaysContentTouches:NO];
[mainScrollView setCanCancelContentTouches:NO];
// 當前螢幕寬度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 當前螢幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.