简体   繁体   English

无需情节提要的表视图控制器中的Detail View Controller?

[英]Detail View Controller from Table View Controller WITHOUT Storyboard?

I have a tableview in a controller that shows receipts and their image, date created, etc. 我在控制器中有一个tableview ,它显示收据及其图像,创建日期等。
I made it so when the user taps a cell, it goes to DetailViewController . 我这样做的目的是,当用户点击一个单元格时,它将转到DetailViewController Using this code: 使用此代码:

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

{
    ReceiptDetailViewController *controller = [[ReceiptDetailViewController alloc] initWithNibName:@"ReceiptDetailViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

I'd like to pass the image, date, category, etc, to my new view in a UIImageView , textView , etc. 我想将图像,日期,类别等传递给UIImageViewtextView等中的新视图。
This is the code for my Table in the first controller: 这是第一个控制器中我的Table的代码:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    Receipt *receipt = [self.receiptsArray objectAtIndex:indexPath.row];

    ReceiptCategory *category = receipt.relationshipToCategory;
    ReceiptImage *image = receipt.relationshipToImage;

    cell.textLabel.text = category.receiptCategory;
    cell.detailTextLabel.text = receipt.date.description;

    if(self.imageCache.count <= indexPath.row) {
        [self.imageCache addObject:[UIImage imageWithData:image.data]];
    }

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    imageView.image = [self.imageCache objectAtIndex:indexPath.row];

    cell.imageView.image = imageView.image;

    return cell;
}

How do I pass this data? 如何传递这些数据? I have searched around for two days now and all solutions include a storyboard, which I am not using. 我搜索了大约两天,所有解决方案都包括一个故事板,但我没有使用。

You should just use Table View Delegate Method 您应该只使用Table View Delegate方法

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

There you can retrieve the selected receipt and pass the data to the detail view like that: 您可以在那里检索选定的收据并将数据传递到详细信息视图,如下所示:

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

{
    ReceiptDetailViewController *controller = [[ReceiptDetailViewController alloc] initWithNibName:@"ReceiptDetailViewController" bundle:nil];
    Receipt *selectedReceipt = [self.receiptsArray objectAtIndex:indexPath.row];
    controller.receipt = selectedReceipt;
    [self.navigationController pushViewController:controller animated:YES];
}

You can create a custom class for your detailed view controller and create an object of the same when the user clicks on any cell. 您可以为详细视图控制器创建自定义类,并在用户单击任何单元格时创建相同的对象。 Something like this 像这样

DetailViewControllerClass *newObject = [[DetailViewControllerClass alloc] init];
newObject.data1 = dataToBeSent ; //assign data 

You should get reference to your receipt in didSelectRowAtIndexPath: and pass it to ReceiptDetailViewController. 您应该在didSelectRowAtIndexPath:中获得对收据的引用,并将其传递给ReceiptDetailViewController。 I assume you have property to accept the data in ReceiptDetailViewController: 我假设您具有接受ReceiptDetailViewController中的数据的属性:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get reference to receipt
    Receipt *receipt = [self.receiptsArray objectAtIndex:indexPath.row];

    ReceiptDetailViewController *controller = [[ReceiptDetailViewController alloc] initWithNibName:@"ReceiptDetailViewController" bundle:nil];

    // Pass data to controller
    controller.receipt = receipt;
    [self.navigationController pushViewController:controller animated:YES];
}

It this example you should create property in ReceiptDetailViewController.h file: 在此示例中,您应该在ReceiptDetailViewController.h文件中创建属性:

@property(nonatomic, strong) Receipt *receipt;

Now you can use it in your ReceiptDetailViewController object. 现在,您可以在ReceiptDetailViewController对象中使用它。

You should make an object of Receipt in ReceiptDetailViewController and make the property nonatomic and strong Than in tour view controller Write this

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Receipt *receipt = [self.receiptsArray objectAtIndex:indexPath.row];

 ReceiptDetailViewController *controller = [[ReceiptDetailViewController alloc] initWithNibName:@"ReceiptDetailViewController" bundle:nil];
controller.objReceipt= receipt;
    [self.navigationController pushViewController:controller animated:YES];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM