2016-07-11 6 views
2

ここでは、私のラベルをtableviewから更新しようとしています。私は間違ったことをしており、それを理解することはできません。別のテーブルビューでラベル付けする。だから、私は詳細ビューコントローラからperformSegueと呼ばれる私のテーブルビューからラベルを更新する方法、objective-c

#import "ViewController.h" 
#import "TechStars.h" 
#import "InfoViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.techstars = [TechStars stars]; 

    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [self.techstars count]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return 80; 
} 

- (UIImage *)imageScaledToSize:(UIImage *)image size:(CGSize)newSize { 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

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

    static NSString *CellIdentifier = @"employeeCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    [[cell detailTextLabel] setNumberOfLines:0]; // unlimited number of lines 
    [[cell detailTextLabel] setLineBreakMode:NSLineBreakByWordWrapping]; 
    [[cell detailTextLabel] setFont:[UIFont systemFontOfSize: 12.0]]; 

    NSDictionary *employee = self.techstars[indexPath.row]; 
    cell.textLabel.text = [employee objectForKey:@"employeename"]; 
    cell.detailTextLabel.text = [employee objectForKey:@"position"]; 
    cell.imageView.image = [self imageScaledToSize :[employee objectForKey:@"profilePicture"] size:CGSizeMake(95, 75)]; 


    return cell; 

} 

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([sender isKindOfClass:[UITableViewCell class]]) 
    { 
     if ([segue.destinationViewController isKindOfClass:[InfoViewController class]]) 
     { 
      InfoViewController *nextViewController = segue.destinationViewController; 
      NSIndexPath *path = [self.tableView indexPathForCell:sender]; 
      TechStars *selectedObject = [self.techstars objectAtIndex:path.row]; 
      // this also can be written as self.techstars[path.row]; literal syntax 
      nextViewController.stars = selectedObject; 
     } 
    } 
} 


InfoViewController.h 
// Test 
// 
// Created by 123 on 10/07/16. 
// Copyright © 2016 123. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import "TechStars.h" 
#import "ViewController.h" 

@interface InfoViewController : UIViewController 

@property (strong, nonatomic) TechStars *stars; 

@property (strong, nonatomic) IBOutlet UILabel *employeeName; 

@property (strong, nonatomic) IBOutlet UIImageView *employeeImage; 


@property (strong, nonatomic) IBOutlet UILabel *employeeDuties; 


@end 


// InfoViewController.m 
// Test 
// 
// Created by 123 on 10/07/16. 
// Copyright © 2016 123. All rights reserved. 
// 

#import "InfoViewController.h" 
#import "ViewController.h" 

@interface InfoViewController() 

@end 

@implementation InfoViewController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 


} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

にテーブルビューからのデータを送信しようとしていますか? –

+0

'prepareForSegue'にブレークポイントを設定して何が起きているのか確認しましたか?たぶんあなたの 'if'ステートメントが一致しないかもしれません。また、デスティネーションビューコントローラで「星」と何もしないようです。 – Paulw11

+0

正確にどのラベルを更新しようとしていますか? 'InfoViewController'のどれか?どこ? 'prepareForSegue:sender:'で? YESならば、 'IBOulet UILabel'がまだnilであるからです。値を 'InfoViewController'の' NSString'プロパティにセットし、その値を 'UILabel'に代入する必要があります。 – Larme

答えて

0
Please try the below code: 
    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 
     if ([segue.identifier isEqualToString:@"secondView"]) 
     { 
      UINavigationController* controller = [segue destinationViewController]; 
      NSArray *viewControllers = controller.viewControllers; 
      SecondViewController* viewController = [viewControllers objectAtIndex:0]; 

      NSIndexPath *path = [self.tableView indexPathForCell:sender]; 
      TechStars *selectedObject = [self.techstars objectAtIndex:path.row]; 

      viewController.stars = selectedObject ; 
     } 
    } 
関連する問題