[英]Horizontal scrolling of tableview with swipe gesture left and right
我正在嘗試通過水平滾動實現uitableview
。 我被帶上scrollview並在scrollview上添加了tableview。 我將滾動視圖的內容大小設置為大於框架大小。 使它可以水平滾動。 但是,當我在uitableview上添加滑動手勢時,它不執行操作。
代碼段在這里
leftswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
leftswipe.delegate = self;
leftswipe.direction = UISwipeGestureRecognizerDirectionLeft;
rightswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
rightswipe.delegate = self;
rightswipe.direction = UISwipeGestureRecognizerDirectionRight;
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender
{
if ( sender.direction == UISwipeGestureRecognizerDirectionLeft )
{
NSLog(@" *** SWIPE LEFT ***");
index1++;
self.tableHeaderLabel.text = [self.categoriesLabelArray objectAtIndex:index1];
[self.feedsTableView reloadData];
}
if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
NSLog(@" *** SWIPE RIGHT ***");
index1--;
self.tableHeaderLabel.text = [self.categoriesLabelArray objectAtIndex:index1];
[self.feedsTableView reloadData];
}
}
我想在向左或向右滑動時重新加載表格。 但是目前它不起作用。
如果在scrollView中添加tableView,則scrollRecognizer可能覆蓋了您的gestureRecognizer。
你可以嘗試實現你的代碼
CGFloat lastOffset_x = 0;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat currentOffSet_x = scrollView.contentOffSet.x;
if(lastOffset_x > currentOffSet_x){
//direction left
}else{
//direction right
}
lastOffset_x = scrollView.contentOffSet.x;
}
並自己跟蹤滾動方向。
ps:請記住將scrollView的contentSize調整為tableView.frame.size.width*numberOfYourTableView
。
嘗試添加該UITableView委托方法:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
順便說一句,通過設置direction
屬性,您只能實例化一個UIGestureRecognizer:
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizer:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[tableView addGestureRecognizer:recognizer];
}
當我需要顯示水平表格視圖時,只需使用:
-(void)setupProductsTableView {
self.innerProductsTable.transform = CGAffineTransformMakeRotation(-M_PI_2);
self.innerProductsTable.delegate = self;
self.innerProductsTable.dataSource = self;
}
如果需要跟蹤滾動,請使用:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
int lastVisibleCell = [[[tableView indexPathsForVisibleRows] lastObject ] row];
//do some with last displayed cell
}
問候,Venelin
在SwipeRecognizer中僅編寫以下代碼。
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender
{
if ( sender.direction == UISwipeGestureRecognizerDirectionLeft )
{
NSLog(@" *** SWIPE LEFT ***");
index1++;
[self.feedsTableView reloadData];
}
if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
NSLog(@" *** SWIPE RIGHT ***");
index1--;
[self.feedsTableView reloadData];
}
}
並按以下方式編寫tableview委托方法(用於Data):
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.categoriesLabelArray objectAtIndex:index1];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(index1 == 0)
//set tableCellDataArray
if(index1 == 1)
//set tableCellDataArray
if(index1 == 2)
//set tableCellDataArray
//...
//...
if(index1 == n)
//set tableCellDataArray
}
是的,別忘了將以下內容添加到viewDidLoad方法
- (void)viewDidLoad
{
leftswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
leftswipe.delegate = self;
leftswipe.direction = UISwipeGestureRecognizerDirectionLeft;
rightswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
rightswipe.delegate = self;
rightswipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.feedsTableView addGestureRecognizer: leftswipe];
[self.feedsTableView addGestureRecognizer: rightswipe];
}
如果您的要求是在表格視圖中顯示內容(僅1行,並且在水平滾動時),則最好的方法是轉換表格視圖。 這將解決所有您不需要使用scrollview的問題。
如果要求是其他情況...請詳細說明您的要求。 因此,我們可以找到解決方案。
只需使用UICollectionView
!
將流布局的scrollDirection
屬性更改為UICollectionViewScrollDirectionHorizontal
。
[yourFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.