详细信息视图控制器从表视图控制器无故事板?(Detail View Controller from

2019-10-19 07:36发布

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

我如何通过这些数据? 我现在已经四处搜寻了两天,所有的解决方案包括故事板,这我不使用。

Answer 1:

你应该得到的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对象使用它。



Answer 2:

你应该只使用表视图的委托方法

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


Answer 3:

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

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


Answer 4:

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

}


文章来源: Detail View Controller from Table View Controller WITHOUT Storyboard?