如何在uitableview单元格中选择后将数据传递到详细信息视图

时间:2013-08-20 09:14:29

标签: objective-c cocoa-touch storyboard

我有一个UITableViewController,其中包含一个名为userList的NSMutableArray填充的数据。选择特定用户后,它会转到详细视图,但不会在UITextviewUIImageviewUIView中显示数据。(详情视图)。我正在使用故事板。是我的示例代码。

请给我任何想法。提前谢谢。

Tableviewcontroller.m

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
//  tableData=[NSArray arrayWithObjects:@"Alto",@"Estilo",@"Swift",@"Honda CRV",@"Ritz", nil];

thumbnails=[NSArray arrayWithObjects:@"car1.jpg",@"car2.jpg",@"car3.jpg",@"car4.jpg",@"car5.jpg", nil];

Car *car1=[Car new];
car1.carName=@"Alto";
car1.imageFile=@"Exotic_Car_-_Bugatti.jpg";
car1.carDetails=[NSArray arrayWithObjects:@"Harsha    Mar05,2019",@"Registration   AP2911234",@"Model Year   2009",@"CarModel   Alto",@"Engine   StreamEngine",@"Petrol   Petrol",@"Exterior Color   Black",@"interior Color   Grey",@"Fuel Economy   1000",@"Insurance   Insurance",@"Life time tax   Free", nil];

Car *car2=[Car new];
car2.carName=@"Estilo";
car2.imageFile=@"images.jpg";
car2.carDetails=[NSArray arrayWithObjects:@"Harsha    Mar05,2019",@"Registration   AP2911234",@"Model Year   2009",@"CarModel   Alto",@"Engine   StreamEngine",@"Petrol   Petrol",@"Exterior Color   Black",@"interior Color   Grey",@"Fuel Economy   1000",@"Insurance   Insurance",@"Life time tax   Free", nil];

Car *car3=[Car new];
car3.carName=@"Swift";
car3.imageFile=@"images1.jpg";
car3.carDetails=[NSArray arrayWithObjects:@"Harsha    Mar05,2019",@"Registration   AP2911234",@"Model Year   2009",@"CarModel   Alto",@"Engine   StreamEngine",@"Petrol   Petrol",@"Exterior Color   Black",@"interior Color   Grey",@"Fuel Economy   1000",@"Insurance   Insurance",@"Life time tax   Free", nil];

Car *car4=[Car new];
car4.carName=@"Honda CRV";
car4.imageFile=@"index.jpg";
car4.carDetails=[NSArray arrayWithObjects:@"Harsha    Mar05,2019",@"Registration   AP2911234",@"Model Year   2009",@"CarModel   Alto",@"Engine   StreamEngine",@"Petrol   Petrol",@"Exterior Color   Black",@"interior Color   Grey",@"Fuel Economy   1000",@"Insurance   Insurance",@"Life time tax   Free", nil];

Car *car5=[Car new];
car5.carName=@"Ritz";
car5.imageFile=@"Sport_Super_car.jpg";
car5.carDetails=[NSArray arrayWithObjects:@"Harsha    Mar05,2019",@"Registration   AP2911234",@"Model Year   2009",@"CarModel   Alto",@"Engine   StreamEngine",@"Petrol   Petrol",@"Exterior Color   Black",@"interior Color   Grey",@"Fuel Economy   1000",@"Insurance   Insurance",@"Life time tax   Free", nil];

tableData=[NSArray arrayWithObjects:car1,car2,car3,car4,car5, nil];


}

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

return [tableData count];

}

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

{

 static NSString *simpleTableIdentifier=@"CarCell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

 }


Car *car = [tableData objectAtIndex:indexPath.row];

cell.textLabel.text = car.carName;
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    return cell;

}

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

{

return 78;

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

{
    if ([segue.identifier isEqualToString:@"showcardetails"]) 

{

 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];


CarDetailviewViewController *destViewController = segue.destinationViewController;

destViewController.car = [tableData objectAtIndex:indexPath.row];

 // Hide bottom tab bar in the detail view
destViewController.hidesBottomBarWhenPushed = YES;


}

}

CarDetailviewcontroller.m

@synthesize carPhoto;
@synthesize cardetailsTextView;
@synthesize car;

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.
self.title = car.carName;
self.carPhoto.image = [UIImage imageNamed:car.imageFile];

NSMutableString *ingredientText = [NSMutableString string];

for (NSString* ingredient in car.carDetails) 
{

[ingredientText appendFormat:@"%@\n", ingredient];

}

self.cardetailsTextView.text = ingredientText;

}

4 个答案:

答案 0 :(得分:1)

CarDetailviewcontroller.m中,您必须分配变量类型car

如果carstring而不是你那样做

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
           car = [[NSString all]init];
        }
        return self;
    }

我希望你的代码工作正常后

答案 1 :(得分:0)

如果你正在使用故事板,那么有一种很好的方法:使用segues。

  1. 在故事板中,crtl-从tableview单元格拖动到目标视图控制器。

  2. 选择形成segue的连接线中的圆圈,并在属性编辑器中转到第四部分,并在标识符字段中为segue命名,例如'pushToDetail'。

  3. 在起始表视图控制器的代码中添加一个名为...的方法

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

  4. 在此方法中,您可以使用它接收的segue变量来引用目标视图控制器并向其发送一些数据。例如:

    CarDetailViewController *carDetailVC = segue.destinationViewController;

  5. 然后你可以发送数据,所以如果它有一个数组属性,你可以......

    [carDetailVC setPhoto:[thumbnails objectAtIndex:indexPath.row]];

    1. 所以最后一个问题是你如何触发segue?这是最简单的一点......在表视图控制器中,确保实现方法:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    2. 并且在那个方法中告诉segue如此发生:

      [self performSegueWithIdentifier:@"pushToDetail" sender:self];

      请参阅标识符是您在步骤2中在故事板中指定的标识符。

      这是在视图控制器之间发送数据的好方法。 在适用于iOS的View Controller编程指南中,Apple开发人员网站上有适当的文档。

答案 2 :(得分:0)

试试这个

下载完整的源代码here

在detailView中创建一个属性

@property (strong, nonatomic) id detailItem;

然后在你的Tableviewcontroller类中替换这个

 [[segue destinationViewController] setDetailItem:[tableData objectAtIndex:indexPath.row];];

代替

destViewController.car = [tableData objectAtIndex:indexPath.row];

快乐编码..

答案 3 :(得分:0)

由于detailviewcontroller已经初始化,它不会显示任何更改,在公共方法中添加所有更改并在您创建后调用它:

destViewController.car = [tableData objectAtIndex:indexPath.row];