如何在UITableView中转到下一个单元格(Detail View)?

时间:2012-02-20 14:49:28

标签: objective-c uitableview

所以,我有一个UITableView分为3个部分。我想能够,一旦我打开第一部分(即)中的第二行,向左滑动以转到下一个单元格,并向右滑动以前一个单元格。

我为滑动编写了代码:

SecondDetailView.m

- (void)viewDidLoad
{   
    UISwipeGestureRecognizer *swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDetectedLeft:)];
    swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeRecognizerLeft];
    [swipeRecognizerLeft release];

    UISwipeGestureRecognizer *swipeRecognizerRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDetectedRight:)];
    swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRecognizerRight];
    [swipeRecognizerRight release];
}


- (void)swipeDetectedRight:(UIGestureRecognizer *)sender {

    NSLog(@"Right Swipe");

}

- (void)swipeDetectedLeft:(UIGestureRecognizer *)sender {
    NSLog(@"Left Swipe");
}

我该怎么做?将代码放入详细信息视图是否正确?

3 个答案:

答案 0 :(得分:3)

我为您的问题提供了一个非常简单的解决方案。 您需要在.h文件中声明NSMutableArray *arr;,并在移动到详细信息页面时将阵列分配给此数组。 此外,您还需要声明NSString *currentPos;变量。

- (void)swipeDetectedRight:(UIGestureRecognizer *)sender {

   currentPos--;
   NSMutableDictionary *dic=[arr objectAtIndex:currentPos];


}

- (void)swipeDetectedLeft:(UIGestureRecognizer *)sender {
     currentPos++;
     NSMutableDictionary *dic=[arr objectAtIndex:currentPos];
}

通过这种方式,您可以获得数组的下一个和上一个索引值。

希望对您有所帮助。 Shivam

答案 1 :(得分:1)

在我的示例中,我使用NSString作为我的数据,将在详细视图控制器中显示。随意将其更改为适合您需求的任何内容。好的,我们走了:

首先在DetailViewController中声明一个协议,如下所示:

@class DetailViewController;

@protocol DetailViewControllerDelegate <NSObject>
- (void)swipeToNextCell:(DetailViewController *)sender;
- (void)swipeToPreviousCell:(DetailViewController *)sender;
@end

@interface DetailViewController : UIViewController
@property(weak, nonatomic) id<DetailViewControllerDelegate> delegate;
@property(copy, nonatomic) NSString *data;
@property(weak, nonatomic) IBOutlet UILabel *label;
@end

接下来是在DetailViewController中添加UISwipeGestureRecognizers来检查手势:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedLeft:)];
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftGesture];

    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedRight:)];
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightGesture];
}

实现viewWillAppear以在推送DetailViewController时显示数据:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.label.text = self.data;
}

不要忘记实现GestureRecognizers调用的方法:

- (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"Right Swipe");
    [self.delegate swipeToNextCell:self];
    self.label.text = self.data;
}

- (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"Left Swipe");
    [self.delegate swipeToPreviousCell:self];
    self.label.text = self.data;
}

这就是你在细节视图中所需要的一切。现在转到TableViewController。您的TableViewController应该实现DetailViewControllerDelegate协议:

@interface CustomTableViewController : UITableViewController <DetailViewControllerDelegate>
@property(strong, nonatomic) DetailViewController *detailViewController;
@property(assign, nonatomic) NSInteger currentRow;
@end

这是我的detailViewController @property:

的getter
- (DetailViewController *)detailViewController
{
    if (_detailViewController == nil) 
    {
        _detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        _detailViewController.delegate = self;
    }
    return _detailViewController;
}

以下是我如何管理行选择:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *viewController = self.detailViewController;
    viewController.data = [NSString stringWithFormat:@"Cell: %d", indexPath.row];
    viewController.title = @"Detail";
    self.currentRow = indexPath.row;
    [self.navigationController pushViewController:viewController animated:YES];
}

你要做的最后一件事是实现协议的方法:

- (void)swipeToNextCell:(DetailViewController *)sender
{
    // Get data for next row
    sender.data = [NSString stringWithFormat:@"Cell: %d", ++self.currentRow];

}

- (void)swipeToPreviousCell:(DetailViewController *)sender
{
    // Get data for next row
    sender.data = [NSString stringWithFormat:@"Cell: %d", --self.currentRow];
}

我在模拟器上测试过并且工作正常。这非常简单,因为我的数据模型非常简单 - 它只是NSString。没有检查部分中是否有任何行,所以你必须自己解决这个问题。但整个代表团模式应该是一样的。

祝你好运!

答案 2 :(得分:0)

在详细视图控制器中声明协议,并将父(应该是表视图控制器)设置为委托。然后,在滑动方法中调用委托并实现更改所选行的必要代码。