0

私は最初のアプリを作成しており、ストーリーボードでXCode4を使用しています。この場所、たくさんのチュートリアル、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

これを実行する1つの方法は、「ソートされた」データソース配列を「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

新しいプロパティを合成することを忘れないでください @synthesizedetailsDataSource、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 に答える