2014-02-09 103 views
0

我有一个plist与联系人:根是一个数组,项目0-150是字典,每个字典是一个单一的联系与“名称”,“数字”,和“email”字符串。添加额外的数据到内部数组与排序的外部数组

以下代码按字母顺序将联系人根据“名称”字符串排序为多个部分。然后使用内部数组填充每个部分的单元格。然后我将内部数组的名称传递给我的详细视图。

但是,我不知道如何将每个联系人的正确号码和电子邮件传递到详细视图。我一直在研究这个问题很长一段时间,找不到解决方案。

@interface ContactsViewController() 
-(void)configureSectionData; 
@end 

@implementation ContactsViewController 
@synthesize tableData; 
@synthesize collation; 
@synthesize outerArray; 
@synthesize indexTitlesArray, namesDictionary; 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - Table view methods 

-(void)configureSectionData { 

NSUInteger sectionTitlesCount = [collation.sectionTitles count]; 

self.outerArray = [NSMutableArray arrayWithCapacity:sectionTitlesCount]; 

for (NSUInteger index = 0; index < sectionTitlesCount; index++) { 

    NSMutableArray *array = [NSMutableArray array]; 

    [self.outerArray addObject:array]; 

} 

for (NSString *nameString in tableData) 
{ 
NSInteger sectionNumber = [collation sectionForObject:nameString collationStringSelector:@selector(lowercaseString)]; 
NSMutableArray *sectionNames = [outerArray objectAtIndex:sectionNumber]; 
[sectionNames addObject:nameString]; 
} 

for (NSUInteger index = 0; index < sectionTitlesCount; index++) { 

    NSMutableArray *namesForSection = [outerArray objectAtIndex:index]; 

    NSArray *sortedNamesForSection = [collation sortedArrayFromArray:namesForSection collationStringSelector:@selector(lowercaseString)]; 

    [self.outerArray replaceObjectAtIndex:index withObject:sortedNamesForSection]; 

} 

} 

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

NSString *theLetter = [self.collation.sectionTitles objectAtIndex:section]; 

if (![theLetter isEqualToString:@"#"]) { 
    NSString *titleString = [NSString stringWithFormat:@"%@", theLetter]; 
    return titleString; 
} 

return nil; 
} 

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
return self.collation.sectionTitles; 
} 

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 
return [self.collation sectionForSectionIndexTitleAtIndex:index]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

NSArray *innerArray = [self.outerArray objectAtIndex:section]; 
return [innerArray count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *cellIdentifier = @"cellIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

// Get the inner array for this section 
NSArray *innerArray = [self.outerArray objectAtIndex:indexPath.section]; 

// Get the name from the inner array 
NSString *theName = [innerArray objectAtIndex:indexPath.row]; 

cell.textLabel.text = theName; 

return cell; 

} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainiPhoneStoryboard" bundle:nil]; 
DetailViewController *detailView = (DetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 

// Get the inner array for this section 
NSArray *innerArray = [self.outerArray objectAtIndex:indexPath.section]; 

// Get the name from the inner array 
NSString *tmpname = [innerArray objectAtIndex:indexPath.row]; 
detailView.lblname = tmpname; 

[self presentViewController:detailView animated:YES completion:nil]; 

} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

self.namesDictionary = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"]]; 

self.tableData = [namesDictionary valueForKey:@"name"]; 

self.collation = [UILocalizedIndexedCollation currentCollation]; 

[self configureSectionData]; 

} 

回答

0

既然你从刚刚从你的plist中的名称的数组填充你的表,你就必须使用该名称来查找其所属的字典来搜索阵列,这样你就可以传递到详细视图控制器(您需要在您的详细视图控制器中创建一个属性,在我的示例中为passedInDictionary):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainiPhoneStoryboard" bundle:nil]; 
    DetailViewController *detailView = (DetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 

    // Get the inner array for this section 
    NSArray *innerArray = [self.outerArray objectAtIndex:indexPath.section]; 

    // Get the name from the inner array 
    NSString *tmpname = [innerArray objectAtIndex:indexPath.row]; 
    NSInteger indx = [self.namesDictionary indexOfObjectPassingTest:^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop) { 
     return [dict[@"name"] isEqualToString:tmpname]; 
    }]; 
    NSDictionary *dict = self.namesDictionary[indx]; 
    detailView.passedInDictionary = dict; 

    [self presentViewController:detailView animated:YES completion:nil]; 
} 
+0

这是100%正确的。你教会了我一种传递数据的新方法。我非常感谢你的帮助。我一直试图自己解决这个问题! – user3264086