我正在创建segue,以便将数据从tableview控制器传递到详细视图控制器as shown in the picture。我的表格单元格 有一个UIImage和两个UILabel,我想在详细视图控制器中显示其中三个。但是,当我运行该程序时,它显示为this。
这是tableviewcontroller.m
的segue-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"prepareForSegue:%@", segue.identifier);
if([segue.identifier isEqualToString:@"TeamMembersSegue"]){
NSIndexPath*indexPath = (NSIndexPath*)sender;
ViewController2*vc = ((UINavigationController*)segue.destinationViewController).topViewController;
vc.lawfirmname = [tableData objectAtIndex:indexPath.row];
vc.detail= [detail objectAtIndex:indexPath.row];
vc.lawyerimage = [thumbnails objectAtIndex:indexPath.row];
}
}
和detailviewcontroller.h的代码(视图控制器2)
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@property (nonatomic, strong) NSString *lawfirmname;
@property (strong, nonatomic) IBOutlet UILabel *lawfirm;
@property (nonatomic, strong) NSString *detail;
@property (weak, nonatomic) IBOutlet UITextView *detailtextview;
@property (nonatomic, strong) UIImage*lawyerimage;
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end
和detailviewcontroller.m(查看控制器2)
#import "ViewController2.h"
@interface ViewController2 ()
@end
@implementation ViewController2
@synthesize lawfirmname;
@synthesize lawfirm;
@synthesize detail;
@synthesize detailtextview;
@synthesize lawyerimage;
@synthesize image;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.lawfirm.text = self.lawfirmname;
self.detailtextview.text = self.detail;
self.image.image = self.lawyerimage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
如果有人有任何解决方案,我将不胜感激。如果您不理解这个问题,请通知我。
答案 0 :(得分:1)
使用 didSelectRowAtIndexPath 将数据从tableview控制器传递到详细信息视图控制器。例如: -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedRowModel = [self.someArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"TeamMembersSegue" sender:self];
}
然后覆盖 prepareForSegue 方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"TeamMembersSegue"]){
ViewController2 *vc = segue.destinationViewController;
vc.selectedModel = self.selectedRowModel;
}
}
注意: - 创建一个公共行模型来表示您的各个Row项。 Model将为UITableView中的每一行存储图像URL和图像描述。
答案 1 :(得分:0)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"showlawfirmdetail" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"prepareForSegue:%@", segue.identifier);
if([segue.identifier isEqualToString:@"showlawfirmdetail"]){
NSIndexPath*indexPath = (NSIndexPath*)sender;
ViewController2*vc = (ViewController2*)segue.destinationViewController;
vc.lawfirmname = [tableData objectAtIndex:indexPath.row];
vc.detail= [detail objectAtIndex:indexPath.row];
vc.lawyerimage = [thumbnails objectAtIndex:indexPath.row];
}
}
部分工作。剩下的问题是我无法在视图控制器2中添加图像。它崩溃了。但是,如果我删除代码self.image.image = self.lawyerimage;
,则应用程序可以顺利运行。
答案 2 :(得分:0)
我认为有一件事是可能的,如果你由于某种原因丢失了IBOutlet连接或者它是重复的。检查出来,就像在图像中一样。在右上角看到只有一个推荐出口:
这里是我的“TableViewController.h”代码,我做的一件事就是以编程方式设置NSStrings和UIImage:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"TeamMembersSegue"]){
ViewController2 *vc = segue.destinationViewController;
vc.lawfirmname = @"Title";
vc.detail = @"Detail here";
vc.lawyerimage = [UIImage imageNamed:@"print"];
}
}
我的“ViewController2.h”和你的一样:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.lawfirm.text = self.lawfirmname;
self.detailtextview.text = self.detail;
self.image.image = self.lawyerimage;
}
看,它工作正常:
答案 3 :(得分:0)
我有解决方案: 替换
vc.lawyerimage = [thumbnails objectAtIndex:indexPath.row];
通过
vc.lawyerimage = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
答案 4 :(得分:0)
我建议你在第二个视图控制器中声明三个不同的字符串,然后通过prepareForSegue
方法将单元格的数据传递给这些字符串。在此之后,使用IBOutlets
或viewDidLoad
方法填写这三个字符串中的viewWillAppear
。这解决了我的问题。希望这有用。