在Uitableviewcell中滑动

时间:2011-11-10 09:13:36

标签: iphone uitableview uigesturerecognizer swipe-gesture

大家好,我有一个问题,我们如何在tableview中实现滑动手势功能?我的项目有很多章节,每章都加载在tableview单元格中,我按下按钮点击并放置到下一章它在tableview的页脚中,并转到tableview标题中的上一页按钮。一切顺利,当用户点击按钮时重新加载数据。但是现在我想要在用户向右滑动时滑动tableview单元格中的此功能它想要加载下一章,反之亦然。下一章和prvous章的代码是

-(IBAction)_clickbtntablenextchapter:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
-(IBAction)_clickbtntableprvchapter:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}

我只想在滑动功能中实现它。 提前致谢。 修改

-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer {
    // do whatever you need to do ...
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
-(void) handleSwipeGestureleft:(UISwipeGestureRecognizer*)recognizer {
    // do whatever you need to do ...
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
} 
this is gesture code and my button code is ,in button it works perfect.
-(IBAction)_clickbtntablenext:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
-(IBAction)_clickbtntableprevious:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}

1 个答案:

答案 0 :(得分:4)

在编辑模式下,滑动行为是默认的,您应该查看Inserting and Deleting Rows in Editing Mode

如果您想拥有自己的行为,可以使用UISwipeGestureRecognizer来检测滑动。

编辑:

创建重新整理器:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight // or whatever
[cell addGestureRecognizer:swipeGesture];
[swipeGesture release];

实现目标的回调:

-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer {
    // do whatever you need to do ...
}