如何根据tableviewcell选择将子视图的nsdictionary保存到主视图 [英] How to save nsdictionary of a subview to a mainview based off tableviewcell selection

查看:70
本文介绍了如何根据tableviewcell选择将子视图的nsdictionary保存到主视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在解析一些看起来像这样的xml

I am currently parsing some xml that looks like this

<Rows>
<Row MANUFACTURERID="76" MANUFACTURERNAME="Fondont" ISMANU="F" ISAUTO="F"/>
<Row MANUFACTURERID="18" MANUFACTURERNAME="Anti" ISMANU="T" ISAUTO="T"/>
</Rows>

我解析它以便有一个字典数组(每个字典都有Row的四个值)在它)。

I parse it so that there is an array of dictionaries (each dictionary has the four values of the Row in it).

然后我将ManufacturerName传递给我的startSortingTheArray方法,如此

I then pass ManufacturerName to my startSortingTheArray method like this

if (dataSetToParse == @"ICMfg") // ICMfg is a string passed to this view from the parent view cell selection enabling me to pass different data sets to this view
    {
       //Filter results (ISAUTO = T)
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ISAUTO",@"T"];
        NSArray *filteredArray = [myDataArray filteredArrayUsingPredicate:predicate];
        //Passes Manufacturer strigs over to startSortingtheArray method
        [self startSortingTheArray:[filteredArray valueForKey:@"MANUFACTURER"]];
    }

所以从这里所有的ManufacturerNames作为一个数组发送到我的方法字符串。然后我使用这个数组来设置我的所有section / index-scroller。下面的方法显示了我是如何做到的。

So from here all of the ManufacturerNames are sent to my method as an array of strings. I then use this array to set up all of my sections / index-scroller. The method below shows how I am doing this.

//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSArray *)arrayData
{
    //If you need to sort incoming array alphabetically use this line of code
    //TODO: Check values coming in for capital letters and spaces etc
    sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    //If you want the standard array use this code
    //sortedArray = arrayData;

    self.letterDictionary = [NSMutableDictionary dictionary];
    sectionLetterArray = [[NSMutableArray alloc] init];

    //Index scrolling Iterate over values for future use
    for (NSString *value in sortedArray) 
    {
        // Get the first letter and its associated array from the dictionary.
        // If the dictionary does not exist create one and associate it with the letter.
        NSString *firstLetter = [[value substringWithRange:NSMakeRange(0, 1)] uppercaseString]; //uppercaseString puts lowercase values with uppercase

        NSMutableArray *arrayForLetter = [letterDictionary objectForKey:firstLetter];
        if (arrayForLetter == nil) 
        {
            arrayForLetter = [NSMutableArray array];
            [letterDictionary setObject:arrayForLetter forKey:firstLetter];

            [sectionLetterArray addObject:firstLetter]; // This will be used to set index scroller and section titles
        }
        // Add the value to the array for this letter
        [arrayForLetter addObject:value];
    }      
    //Reload data in table
    [self.tableView reloadData];
}

从这里我做了几件事情,在调用[self.tableView reloadData]; ,主要是我用数组的字符串值设置单元格。

from here I do several things to do with setting up the tableview after [self.tableView reloadData]; is called, The main thing being is that I set the cell up with the string values of the array.

//Display cells with data
    NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
    NSString *key = [keys objectAtIndex:indexPath.row];

    cell.textLabel.text = key;

然后选择单元格后,单元格内的字符串值将被发送回主视图,稍后用作搜索参数......事实是我正在设置几个将用作一个搜索字符串的参数。

when the cell is then selected the string value inside the cell is then sent back to the main view and used later as a search parameter... The thing being is that I am setting up several parameters that will be used as one search string.

回顾我解析的XML

<Rows>
    <Row MANUFACTURERID="76" MANUFACTURERNAME="Fondont" ISMANU="F" ISAUTO="F"/>
    <Row MANUFACTURERID="18" MANUFACTURERNAME="Anti" ISMANU="T" ISAUTO="T"/>
</Rows>

这些是SQl表中具有keyvalue MANUFACTURERID的列的值,该值也可以在其他地方找到我解析的表。我想使用这些键值来限制/优化其他查询,但我无法弄清楚如何将它们传递到我的父视图,我设置了所有搜索参数,这就是我的问题如何保存值的字典与子视图中的用户tableview选择有关。这样我就可以将其中一个或一些值传递回不同数据集的子视图,以限制显示的信息,具体取决于用户以前的选择。

These are the values of columns inside an SQl table that has a keyvalue MANUFACTURERID that is also found in other tables that I parse. I would like to use these key values to restrict/refine other queries but I just cannot figure out how to pass them to my parentview where I set up all of the search parameters, that is my question how can I save the dictionary of values that is related to the users tableview selection from the subview. So that I can then pass one or some of those values back to the subview of a different dataset to restrict the information that is displayed dependent on the users previous selections.

这需要我一个小时才能输入。希望它有意义,我仍然是iOS开发和Objective C的新手,这个概念真的在推动我的能力,在我继续前进并最终得到一些废话,我将不得不在以后修复我希望一个或者你们中的一些人可以将这种类型的经验借给我,这样我就可以第一时间做到这一点:)

This has taken me about an hour to type up. Hopefully it makes sense, I am still fairly new to iOS development and Objective C, and this concept is really pushing my capabilities and before I move on and end up hasing some crap together that I will have to fix later on I am hoping that one or some of you will be able to lend your experience in this type of this to me so I can get this right first time :)

如果你需要我澄清任何事情或者为您提供更多信息,帮助您帮助我告诉我。

If you need me to clarify anything or provide you more information that will help you help me just let me know.

提前致谢!

推荐答案

在视图控制器层次结构中向后传递信息的常见模式是使用委托。您可以通过实现以下方法在您的方案中实现此目的:

The common pattern for passing information backwards in your view controller hierarchy is to use delegation. You can achieve this in your scenario by implementing the following:

1)在SearchParametersViewController中定义一个协议,它代表您的父视图控制器你提到过。

1) Define a protocol in the SearchParametersViewController, which represents your the parent view controller you mentioned.

@protocol SearchParametersViewControllerDelegate <NSObject>
@optional
- (void)searchOptionsSelected:(NSArray *)selectedSearchOptions;
@end

2)符合您的协议SearchOptionsSelectionViewController,表示具有可供选择的选择列表的表视图控制器。确保导入或转发声明定义协议的类(例如SearchParametersViewController)。

2) Conform to that protocol in your SearchOptionsSelectionViewController, which represents the table view controller that has a list of selections to choose from. Make sure to import or forward-declare the class the protocol is defined in (e.g. SearchParametersViewController) .

#import "SearchParametersViewController.h"

@interface SearchOptionsSelectionViewController <SearchParametersViewControllerDelegate>

3)在SearchOptionsSelectionViewController中定义委托属性(假设您正在使用iOS 5.0上的ARC,4.x使用 unsafe_unretained 而不是。使用 assign 如果项目使用手动内存管理)。此委托对象将包含对父视图控制器的引用(例如SearchParametersViewController)。您不希望保留此属性,以避免保留循环/循环引用,其中一个对象引用另一个对象,而该引用又返回第一个引用,并且两个对象都不会被释放。

3) Define a delegate property in your SearchOptionsSelectionViewController (assumes you are using ARC on iOS 5.0, 4.x use unsafe_unretained instead of weak. Use assign if the project is using manual memory management). This delegate object will contain a reference to your parent view controller (e.g. SearchParametersViewController). You do not want this property to be retained as to avoid retain cycles/circular references where one object references another, which in turn has a reference back to the first and neither object is ever deallocated.

@property (nonatomic, weak) id<SearchParametersViewControllerDelegate> delegate;

4)在父视图控制器中实例化SearchOptionsSelectionViewController实例时(SearchParametersViewController) ),将委托属性设置为父视图控制器实例,由 self 关键字表示。这可确保您可以在视图控制器层次结构中向后发送消息(和相应的数据),但对象关系保持松散耦合。此委托协议可以符合任何其他视图控制器,选择视图控制器中没有紧密关系返回到父视图控制器,链接它们的唯一方法是选择视图控制器采用灵活的委托协议。

4) When instantiating the SearchOptionsSelectionViewController instance inside your parent view controller (SearchParametersViewController), set the delegate property to the parent view controller instance as represented by the self keyword. This ensures you can send the message (and corresponding data) backward in your view controller hierarchy, yet the object relationships remain loosely coupled. This delegate protocol could be conformed to in any other view controller, there are no tight relationships in the selection view controller back to the parent view controller, the only thing linking them is the flexible delegate protocol adoption by the selection view controller.

SearchOptionsSelectionViewController *selectionViewController = [[SearchOptionsSelectionViewController alloc] init];
selectionViewController.delegate = self;

5)最后,在您的SearchOptionsSelectionViewController表视图中 -tableView:didSelectRowAtIndexPath:委托方法,通过您在SearchParametersViewControllerDelegate协议中定义的委托方法将对应于所选行的数据传递回父视图控制器(SearchParametersViewController)。您必须使用 -respondsToSelector:方法来确保委托对象实际实现 -searchOptionsSelected:委托方法。要强制执行此实现,请在步骤#1中的协议定义中将方法原型上方的 @optional 更改为 @required self.someDataArray 表示您与选择表视图控制器一起使用的数据源。可以更改发送回父视图控制器的委托协议方法和数据对象的细节,这里重要的是委托模式,并且在任一类的实例之间没有任何紧密耦合的关系,但特别是向后视图控制器层次结构。

5) Finally, in your SearchOptionsSelectionViewController table view's -tableView:didSelectRowAtIndexPath: delegate method, pass the data corresponding to the selected row back to your parent view controller (SearchParametersViewController) via the delegate method you defined in the SearchParametersViewControllerDelegate protocol. You must use the -respondsToSelector: method to ensure that the delegate object actually implements the -searchOptionsSelected: delegate method. To force this implementation, change @optional to @required above the method prototype in the protocol definition in step #1. self.someDataArray represents a the data source you are using with the selection table view controller. The specifics of the delegate protocol method and data object(s) sent back to the parent view controller can be changed, the important thing here is the delegation pattern and not having any tightly coupled relationships between the instances of either class, but especially backwards in the view controller hierarchy.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(searchOptionsSelected:)])
    {
        NSArray *selectedObjs = [NSArray arrayWithObject:[self.someDataArray objectAtIndex:indexPath.row]];
        [self.delegate searchOptionsSelected:selectedObjs]
    }
}

6) SearchOptionsSelectionViewController.m中实现委托方法

- (void)searchOptionsSelected:(NSArray *)selectedSearchOptions
{
    // do what you need to with selectedSearchOptions array
}

进一步阅读:

Cocoa Fundamentals指南 - 代表和数据来源

Cocoa Core Competencies - Protocol

这篇关于如何根据tableviewcell选择将子视图的nsdictionary保存到主视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆