通过分段tableview中的segue传递数据

时间:2012-11-28 16:13:30

标签: objective-c ios xcode

在我的tableView中实现部分之后,我无法按正确的顺序将对象传递给详细视图。我的数据源是一个plist,结构为带有字典的数组,它被提取并填充UITableView,如下所示:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Aquavit.plist"];
objArr = [[NSMutableArray alloc] initWithContentsOfFile:path];
[self sectionSort];
}

-(void)sectionSort{  
    self.sections = [[NSMutableDictionary alloc] init];
    BOOL found;
    for (NSDictionary *aqu in objArr)
    {
        NSString *type = [aqu objectForKey:@"Type"];
        found = NO;
        for (NSString *str in [self.sections allKeys])
        {
            if ([str isEqualToString:type])
            {
                found = YES;
            }
        }
        if (!found)
        {[self.sections setValue:[[NSMutableArray alloc] init] forKey:type];}
    }
    for (NSDictionary *aqu in objArr)
    {[[self.sections objectForKey:[aqu objectForKey:@"Type"]] addObject:aqu];}
    for (NSString *key in [self.sections allKeys])
    {[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Type" ascending:YES]]];}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.sections allKeys] count]; 
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}

- (AquavitCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"AquaCell";
AquavitCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
    cell = [[AquavitCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *aqu = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.nameLabel.text = [aqu objectForKey:@"Navn"];
}

然后是segue和问题。在我制作各个部分之前,我通过了下面的整个objArr,以便能够在详细视图中使用代码detailIndex++ / detailIndex--向左/向右滑动数组:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


if ([[segue identifier] isEqualToString:@"DetailSegue"]) {
    NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
    DetailViewController *detailViewController = [segue destinationViewController];
    detailViewController.objArr = [[NSMutableArray alloc] initWithArray:objArr];
    detailViewController.detailIndex = selectedRowIndex.row;
    detailViewController.hidesBottomBarWhenPushed = YES;
}
}

为了简化并避免重新构造整个详细视图,我想用detailViewController.objArr字典中的所有字典初始化sections,并在行时在详细视图中显示正确的对象被选中。对此有任何可能的解决方案吗?

0 个答案:

没有答案