作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我尝试从 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)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
最佳答案
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/
我是一名优秀的程序员,十分优秀!