2012-03-29 8 views
0

Ich möchte in eine Nib-Datei namens DetailView wechseln, anstatt die Warnung zu erhalten. Ich kann nicht herausfinden, wie man das funktioniert. Ich möchte die Daten aus der Reihe mit mir zur neuen Ansicht bringen. Ich habe wirklich versucht, dies zur Arbeit zu bringen, aber ich kann es nicht herausfinden.So gelangen Sie zur detaillierten Detailansicht anstelle einer Warnung in der Tabellenansicht

#import "TabBarSecondViewController.h" 
#import "NSDictionary+MutableDeepCopy.h" 
@implementation TabBarSecondViewController 

@synthesize names; 
@synthesize keys; 
@synthesize table; 
@synthesize search; 
@synthesize allNames; 
@synthesize isSearching; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    self.title = NSLocalizedString(@"Sök Mat", @"Sök Mat"); 
    //self.tabBarItem.image = [UIImage imageNamed:@"second"]; 
} 
return self; 
} 


#pragma mark - 
#pragma mark Custom Methods 
- (void)resetSearch { 
self.names = [self.allNames mutableDeepCopy]; 
NSMutableArray *keyArray = [[NSMutableArray alloc] init]; 
[keyArray addObject:UITableViewIndexSearch]; 
[keyArray addObjectsFromArray:[[self.allNames allKeys] 
           sortedArrayUsingSelector:@selector(compare:)]]; 
self.keys = keyArray; 
} 

- (void)handleSearchForTerm:(NSString *)searchTerm { 
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init]; 
[self resetSearch]; 

for (NSString *key in self.keys) { 
    NSMutableArray *array = [names valueForKey:key]; 
    NSMutableArray *toRemove = [[NSMutableArray alloc] init]; 
    for (NSString *name in array) { 
     if ([name rangeOfString:searchTerm 
         options:NSCaseInsensitiveSearch].location == NSNotFound) 
      [toRemove addObject:name]; 
    } 
    if ([array count] == [toRemove count]) 
     [sectionsToRemove addObject:key]; 

    [array removeObjectsInArray:toRemove]; 
} 
[self.keys removeObjectsInArray:sectionsToRemove]; 
[table reloadData]; 
} 

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

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" 
               ofType:@"plist"]; 
NSDictionary *dict = [[NSDictionary alloc] 
         initWithContentsOfFile:path]; 
self.allNames = dict; 

[self resetSearch]; 
[table reloadData]; 
[table setContentOffset:CGPointMake(0.0, 44.0) animated:NO];  

} 

- (void)viewDidUnload 
    { 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
self.table = nil; 
self.search = nil; 
self.allNames = nil; 
self.names = nil; 
self.keys = nil;} 

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

#pragma mark - 
#pragma mark Table View Data Source Methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return ([keys count] > 0) ? [keys count] : 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
if ([keys count] == 0) 
    return 0; 
NSString *key = [keys objectAtIndex:section]; 
NSArray *nameSection = [names objectForKey:key]; 
return [nameSection count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 

NSString *key = [keys objectAtIndex:section]; 
NSArray *nameSection = [names objectForKey:key]; 

static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
         SectionsTableIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:SectionsTableIdentifier]; 
} 

cell.textLabel.text = [nameSection objectAtIndex:row]; 
return cell; 
} 

- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section { 
if ([keys count] == 0) 
return nil; 

NSString *key = [keys objectAtIndex:section]; 
if (key == UITableViewIndexSearch) 
return nil; 
return key; 
} 
/* 
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
if (isSearching) 
return nil; 
return keys; 
} 
*/ 
#pragma mark - 
#pragma mark Table View Delegate Methods 
- (NSIndexPath *)tableView:(UITableView *)tableView 
willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[search resignFirstResponder]; 
isSearching = NO; 
search.text = @""; 
[tableView reloadData]; 
return indexPath; 
} 

#pragma mark - 
#pragma mark Search Bar Delegate Methods 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
NSString *searchTerm = [searchBar text]; 
[self handleSearchForTerm:searchTerm]; 
} 

- (void)searchBar:(UISearchBar *)searchBar 
textDidChange:(NSString *)searchTerm { 
if ([searchTerm length] == 0) { 
    [self resetSearch]; 
    [table reloadData]; 
    return; 
} 
[self handleSearchForTerm:searchTerm]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
isSearching = NO; 
search.text = @""; 
[self resetSearch]; 
[table reloadData]; 
[searchBar resignFirstResponder]; 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
isSearching = YES; 
[table reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
sectionForSectionIndexTitle:(NSString *)title 
      atIndex:(NSInteger)index { 
NSString *key = [keys objectAtIndex:index]; 
if (key == UITableViewIndexSearch) { 
    [tableView setContentOffset:CGPointZero animated:NO]; 
    return NSNotFound; 
} else return index; 
} 

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

NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 

NSString *key = [keys objectAtIndex:section]; 
NSArray *nameSection = [names objectForKey:key]; 
NSString *selectedCell = [nameSection objectAtIndex:row]; 
NSLog(@"Log selectedCell: %@", selectedCell); 

NSString *msg = [[NSString alloc] initWithFormat: 
       @"You have selected %@", selectedCell]; 
UIAlertView *alert = 
[[UIAlertView alloc] initWithTitle:@"State selected" 
          message:msg 
          delegate:self 
       cancelButtonTitle:@"OK" 
       otherButtonTitles:nil]; 

[alert show];   

} 


@end 

Antwort

0

Haben Sie einen UIViewController für die DetailView-NIB erstellt? Wenn dies der Fall ist, erstellen Sie im UIViewController einige Eigenschaften, um die Daten zu speichern, die Sie in die neue Ansicht übertragen möchten. Stellen Sie sicher, dass Sie den neuen View-Controller zuweisen und alle erforderlichen Verbindungen und Delegaten im View-Controller der Nib in IB festlegen.

Dann instanziieren Sie den neuen UIViewController, in dem Sie die UIAlertView aufrufen.

UIViewController *myVC = [[UIViewController alloc] init]; 
id myValueOne = [myDataOne objectAtIndex:indexPath.row]; 
id myValueTwo = [myDataTwo objectAtIndex:indexPath.row]; 
myVC.propertyOne = myValueOne; 
myVC.propertyTwo = myValueTwo; 
[self.navigationController pushViewController:myVC animated:YES]; 

Hoffe, das hilft.

+0

Vielen Dank für Ihre Hilfe. Ich bekomme es immer noch nicht zur Arbeit. Ich bin ein wenig unsicher, wie und wo ich navigationController deklariere, um dies zum Laufen zu bringen. – user1301000

+0

In der Dokumentation zu UIViewController und UINavigationController finden Sie weitere Informationen. Der Navigationscontroller ist eine Eigenschaft von UIViewController und sollte in den meisten Fällen bereits vorhanden sein. Sie müssen es nur anrufen. – Rob

+0

Danke nochmal. Ich bin nahe dran :) Ich habe die Detailansichtsseite mit einem NSLog eingerichtet. Wenn ich laufe, kann ich sehen, dass die Daten in die Detailansicht in der Konsole gelangen, aber die Ansicht ändert sich nicht. Habe ich etwas in IB verpasst? – user1301000

Verwandte Themen