OStack程序员社区-中国程序员成长平台

标题: ios - 如何从我的 TableView 更新标签,objective-c [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:56
标题: ios - 如何从我的 TableView 更新标签,objective-c

这是我试图从 tableview 更新我的标签。我做错了什么,无法把握。在不同的表格 View 中添加标签。所以,我正在尝试将数据从 TableView 发送到详细 View Controller

#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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {

    return [self.techstars count];
}

-(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath {

    return 80;
}

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

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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) prepareForSegueUIStoryboardSegue *)segue senderid)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



Best Answer-推荐答案


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

关于ios - 如何从我的 TableView 更新标签,objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38302245/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4