• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何从我的 TableView 更新标签,objective-c

[复制链接]
菜鸟教程小白 发表于 2022-12-11 16:56:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我试图从 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap