0
  1. 私のアプリにはUITableViewControllerとDetailViewControllerがあります。
  2. マルチセル選択と詳細アクセサリボタンを備えたUITableViewController。
  3. ユーザーが複数のセルを選択し、[合計]ボタンをタップすると、アラートメッセージにコースの合計価格が表示されます。
  4. そして、ユーザーが詳細アクセサリボタンをタップすると、DetailViewControllerにセグエします。

さて、私の問題は、コードでそれをどのように行うことができるかということです。

UITableViewController.m

    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        [self configureCell:cell atIndexPath:indexPath];

        return cell;


    }


    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
    {
        NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
        cell.textLabel.text = [[object valueForKey:@"courseCode"] description];
        cell.detailTextLabel.text = [[object valueForKey:@"courseName"] description];

    }

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

        if ([[segue identifier] isEqualToString:@"showDetail"]) {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
            [[segue destinationViewController] setDetailItem:object];
        }            
    }

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{



}


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];


        if ([[[object valueForKey:@"creditHours"] description]isEqualToString: @""]) {

        }
        //some thing doing if the user deselect the cell..



    }

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

        if ([[[object valueForKey:@"creditHours"] description]isEqualToString: @""]) {

        }

        //some thing doing if the user deselect the cell..

    }

    - (IBAction)doneButton:(id)sender {

        given the total...

    }

DetailViewController.h

@property (strong, nonatomic) id detailItem;

@property (weak, nonatomic) IBOutlet UITextField *courseCodeLabel;
@property (weak, nonatomic) IBOutlet UITextField *courseNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *creditHoursLabel;
@property (weak, nonatomic) IBOutlet UITextField *preRequisitesLabel;
@property (weak, nonatomic) IBOutlet UITextField *coursePriceLabel;
@property (weak, nonatomic) IBOutlet UITextField *courseEPPLabel;
@property (weak, nonatomic) IBOutlet UITextView *courseDetailLabel;

DetailViewController.m

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController

@synthesize detailItem = _detailItem;
@synthesize courseCodeLabel = _courseCodeLabel;
@synthesize courseNameLabel = _courseNameLabel;
@synthesize creditHoursLabel = _creditHoursLabel;
@synthesize preRequisitesLabel = _preRequisitesLabel;
@synthesize coursePriceLabel = _coursePriceLabel;
@synthesize courseEPPLabel = _courseEPPLabel;
@synthesize courseDetailLabel = _courseDetailLabel;


#pragma mark - Managing the detail item



- (void)setDetailItem:(id)newDetailItem
{
    [self configureView];

    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.courseCodeLabel.text = [[self.detailItem valueForKey:@"courseCode"] description];
        self.courseNameLabel.text = [[self.detailItem valueForKey:@"courseName"] description];
        self.creditHoursLabel.text = [[self.detailItem valueForKey:@"creditHours"] description];
        self.preRequisitesLabel.text = [[self.detailItem valueForKey:@"preRequisites"] description];
        self.coursePriceLabel.text = [[self.detailItem valueForKey:@"coursePrice"] description];
        self.courseEPPLabel.text = [[self.detailItem valueForKey:@"courseEPP"] description];
        self.courseDetailLabel.text = [[self.detailItem valueForKey:@"courseDetails"] description];

    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureView];
}

セグエが通常の方法で正常に機能していることに注意してください。

4

1 に答える 1

0

現在、あなたのセグエはテーブルビューセル(そのプロトタイプ)を詳細コントローラーにリンクしていると思います。代わりに、テーブルビューコントローラ自体(つまり全体)を詳細コントローラにリンクしてみることができます。(これを行うには、テーブルビューコントローラの下部からCtrlキーを押しながらドラッグします)

次に、通常どおりメソッドを処理しますが、実際にセグエをトリガーするには-prepareForSegueを呼び出す必要があります。-performSegueセルをタップしても効果はありません。したがって、-tableView:tableViewaccessory....メソッドでは、呼び出しperformSegueてナビゲーションを実行し、didSelectRow..必要に応じてメソッドを処理できます。

于 2012-08-18T20:01:21.030 に答える