详细信息视图控制器从表视图控制器没有故事板?

时间:2014-02-07 11:05:17

标签: ios objective-c uitableview cocoa-touch

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

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

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

我想将图片,日期,类别等传递到我UIImageViewtextView等新视图中。 这是第一个控制器中我的表的代码:

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

如何传递此数据?我现在已经搜索了两天,所有解决方案都包含一个故事板,我没有使用。

4 个答案:

答案 0 :(得分:0)

您应该只使用表视图委派方法

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您应该在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对象中使用它。

答案 3 :(得分:0)

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

}