0

我正在制作我的第一个应用程序,并且正在使用带有 Storyboard 的 XCode 4。多亏了这个地方、大量的教程、Apple 的档案数据库和一些我,我正在慢慢地掌握基础知识。这是一个从 plist 填充的应用程序。plist 是一个带有字典的数组,字典中包含有关挪威不同红酒的信息。首先,plist 填充了一个 TableView。我正在使用 NSSortDescritor 对 TableView 进行排序,并在导航栏中添加了一个按钮,以便在我希望它由另一个值显示时使用。它看起来像这样:

RootTableViewController.h:

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController <UIActionSheetDelegate> {
NSMutableArray *sortedObjects;
}

-(IBAction)sortButtonPressed:(id)sender;

@end

RootTableViewController.m:

#import "RootTableViewController.h"
#import "ObjectCell.h"
#import "DetailViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController

- (IBAction)sortButtonPressed:(id)sender;

{
    UIActionSheet *sort = [[UIActionSheet alloc]
   //InitWithStyle etc for sheet
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
 NSSortDescriptor *sortDesc;

if (buttonIndex == 0) {
        sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
        [sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
        }
if (buttonIndex == 1) {
        sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES];
        [sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
}
[self.tableView reloadData];
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSString *myfile = [[NSBundle mainBundle]
                    pathForResource:@"Objects" ofType:@"plist"];

sortedObjects = [[NSMutableArray alloc]initWithContentsOfFile:myfile];

NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Popularity" ascending:YES];
[sortedObjects sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (void)viewWillAppear:(BOOL)animated {
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [sortedObjects count];
}

//(I’m using a Custom Cell for the TableView)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"objectCell";

    ObjectCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

    cell = [[[ObjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    cell.nameLabel.text = [[sortedObjects objectAtIndex:indexPath.row] valueForKey:@"Name"];
    cell.countryLabel.text = [[sortedObjects objectAtIndex:indexPath.row] valueForKey:@"Country"];
    return cell;
}

#pragma mark - Table view delegate
//Then the selected object is sent to the DetailViewController in this segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([[segue identifier] isEqualToString:@"DetailSegue"]) {

        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        DetailViewController *detailViewController = [segue destinationViewController];

        detailViewController.selectedObject = [sortedObjects objectAtIndex:selectedRowIndex.row];
    }
}



@end

然后 DetailViewController 接收所选对象以使用其中的数据填充 Labels 和 ImageViews。

DetailViewController.h:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UILabel *districtLabel;
@property (nonatomic, strong) IBOutlet UILabel *countryLabel;
@property (nonatomic, strong) IBOutlet UIImageView *bottleImageView;

@property (nonatomic, strong) NSString *selectedObject;

@end

DetailViewController.m:

#import "WinesDetailViewController.h"

@interface WinesDetailViewController ()

@end

@implementation WinesDetailViewController

@synthesize districtLabel,countryLabel,bottleImageView;

@synthesize selectedObject;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}


- (void)viewDidAppear:(BOOL)animated
{
    self.title = [selectedObject valueForKey:@"Name"];
    [super viewDidAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    districtLabel.text = [selectedObject valueForKey:@"District"];
    countryLabel.text = [selectedObject valueForKey:@"Country"];
    bottleImageView.image = [UIImage imageNamed:[selectedObject valueForKey:@"Image"]];

self.navigationController.navigationBar.translucent = YES;
self.wantsFullScreenLayout = YES;

//Then I’ve added recognizers for left and right swiping:

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];


}

//And the voids to handle the swipes:

- (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
{
//Access previous cell in TableView
}

- (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
{
//Access next cell in TableView
}


//Some more besic code for the view..

@end

如您所见,我在 DetailViewController 中添加了 UISwipeGestureRecognizers,因为我想在向右滑动时使用上一个单元格的数据重新加载它,并在向左滑动时重新加载下一个单元格。现在我不知道如何处理检测到的滑动空白,如何从 DetailView 到达 selectedRowIndex 并滑动单元格?我是编程新手,很长时间以来一直在尝试解决这个问题,所以代码示例会很棒,所以如果你知道我的意思,答案不会导致 100 个新问题。如果你能帮助我,非常感谢。

4

1 回答 1

3

解决此问题的一种方法是通过“prepareForSegue”方法将“排序”数据源数组传递给 DetailViewController 和索引路径。

RootTableViewController.h:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([[segue identifier] isEqualToString:@"DetailSegue"]) {

        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        DetailViewController *detailViewController = [segue destinationViewController];

        detailViewController.selectedObject = [sortedObjects objectAtIndex:selectedRowIndex.row];

        //added code
        detailViewController.detailsDataSource = [[NSArray alloc]initWithArray:sortedObjects];
        detailViewController.detailIndex = selectedRowIndex.row;
    }
}

然后您可以重新加载 DetailViewController 的 UI 元素。这是新属性的声明。

DetailViewController.h:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UILabel *districtLabel;
@property (nonatomic, strong) IBOutlet UILabel *countryLabel;
@property (nonatomic, strong) IBOutlet UIImageView *bottleImageView;

@property (nonatomic, strong) NSString *selectedObject;

// added code
@property (strong, nonatomic) NSArray *detailsDataSource;
@property int detailIndex;
@end

不要忘记合成新属性 @synthesize detailsDataSource,detailIndex;

//And the voids to handle the swipes:

- (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
{
//Access previous cell in TableView
    if (detailIndex != 0) // This way it will not go negative
          detailIndex--;  

    districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"District"]];
    countryLabel.text =  [[detailsDataSource objectAtIndex: detailIndex]  valueForKey:@"Country"];
    bottleImageView.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Image"]];
}

- (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
{
//Access next cell in TableView
    if (detailIndex != [detailsDataSource count]) // make sure that it does not go over the number of objects in the array.
    detailIndex++;  // you'll need to check bounds 

    districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"District"]];
    countryLabel.text =  [[detailsDataSource objectAtIndex: detailIndex]  valueForKey:@"Country"];
    bottleImageView.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Image"]];
}


//Some more besic code for the view..

@end

试试这个,它可能对你有用。否则,我想您可能想看看 Scrollview 和“分页”。iPhone/iPad 用户已经习惯了这种 UI 设计,您也许可以对其进行修改以适应您正在做的事情。

于 2012-07-16T23:57:39.403 回答