通过UITableViewcell推送带有展开segue的数据

时间:2015-01-02 15:29:00

标签: ios xcode uitableview segue

我有一个push segue,用于将数据从uitableview单元格传输到文本字段。应用计划是用户将数据输入文本字段,然后他们将点击一个按钮,它将模态转换到表视图控制器,然后他们将选择一个uitableviewcell,它将被转移回原始视图他们将数据输入到文本字段中,tableviewcell内容将输入到同一视图的不同文本字段中。但我遇到的问题是,数据从原始文本字段中删除,并且正在创建另一个视图。

所以现在,我尝试使用展开segue,所以现在输入的数据显示出来但是表视图单元格没有像以前那样填充。

以下是推送数据的代码 -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = nil;
        Recipe *recipe = nil;
        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            recipe = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            recipe = [recipes objectAtIndex:indexPath.row];
        }


PersonDetailTVC *destViewController = segue.destinationViewController;
        destViewController.recipe = recipe;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

以下是展开segue的代码 -

- (IBAction)unwindFromDetailViewController:(UIStoryboardSegue *)segue
{
    // ViewController *detailViewController = [segue sourceViewController];
    NSLog(@"%@", segue.identifier);
}

1 个答案:

答案 0 :(得分:0)

从评论中扩展,开始使用数据模型对象:

AppDataModel.h

#import <Foundation/Foundation.h>

@class Recipe;

@interface AppDataModel : NSObject

+ (instancetype)sharedData;

- (void)selectRecipeAt:(NSUInteger)index;

@property (nonatomic, strong, readonly) Recipe *selectedRecipe;

@end

AppDataModel.m

#import "AppDataModel.h"

@interface AppDataModel ()

@property (nonatomic, strong) NSArray *recipes;
@property (nonatomic, strong, readwrite) Recipe *selectedRecipe;

@end

@implementation AppDataModel

+ (instancetype)sharedData {
    static AppDataModel *dataModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataModel = [[AppDataModel alloc] init];
    });
    return dataModel;
}

- (void)selectRecipeAt:(NSUInteger)index {
    self.selectedRecipe = [self.recipes objectAtIndex:index];
}

@end

用法:

[[AppDataModel sharedData] selectRecipeAt:0];
Recipe *toDisplay = [[AppDataModel sharedData] selectedRecipe];

这假设您在数据模型中也有代码来设置配方数组。