繁体   English   中英

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

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

我在控制器中有一个tableview ,它显示收据及其图像,创建日期等。
我这样做的目的是,当用户点击一个单元格时,它将转到DetailViewController 使用此代码:

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

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

我想将图像,日期,类别等传递给UIImageViewtextView等中的新视图。
这是第一个控制器中我的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;
}

如何传递这些数据? 我搜索了大约两天,所有解决方案都包括一个故事板,但我没有使用。

您应该只使用Table View Delegate方法

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

您可以在那里检索选定的收据并将数据传递到详细信息视图,如下所示:

- (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];
}

您可以为详细视图控制器创建自定义类,并在用户单击任何单元格时创建相同的对象。 像这样

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

您应该在didSelectRowAtIndexPath:中获得对收据的引用,并将其传递给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];
}

在此示例中,您应该在ReceiptDetailViewController.h文件中创建属性:

@property(nonatomic, strong) Receipt *receipt;

现在,您可以在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