2014-02-07 100 views
1

我有一个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; 
} 

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

回答

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对象使用它。

+0

您必须将此行添加到ReceiptDetailViewController.h @property(nonatomic,strong)Receipt * receipt;你是什​​么意思,你尝试添加@property,但它不起作用? – Greg

+0

嗨格雷格,感谢您抽出时间帮助我,非常感谢。我添加了'@property(nonatomic,strong)Receipt * receipt; '添加到我的detailviewcontroller的头文件中,但它返回两个错误 - 未知类型名称'Receipt'和'带有保留或强属性的属性必须是对象类型'。我很抱歉,如果这是一个愚蠢的问题,但我将如何解决这些问题?谢谢 – user3127576

+0

将#import“Receipt.h”添加到同一文件的顶部。它应该有所帮助。 – Greg

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

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

DetailViewControllerClass *newObject = [[DetailViewControllerClass alloc] init]; 
newObject.data1 = dataToBeSent ; //assign data 
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]; 

} 
相关问题