2012-11-03 35 views
1

我已經搜索了互聯網,並且完全沒有這方面的結果。我的iOS應用程序只是一個帶有indexpath的簡單導航控制器,它應該將所選定製動態單元格的行標題發送給uilabel。問題在於數據源來自mysql,它是在nsarray(稱爲coolarray)中解析的xml。錯誤發生在移動到下一個視圖之前,並在日誌中爲:由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'[__NSDictionaryI isEqualToString:]:發送到實例0x8887670的無法識別的選擇器'。但是,它確實給出了indexPathrow的nslog結果:0或1或任何被選中的結果。將數組傳遞給uilabel一定是個問題,但我無法弄清楚!我遺漏了自定義單元的代碼,因爲這不太可能是錯誤的原因,並且工作正常(加上簡單)。這是我的一個早期問題的後續文件,在UITableView refusing to go to detailViewController,但該代碼不再相關,因爲,正如你可以看到,我的代碼已經發生了很大的變化。從與索引路徑相關的互聯網上的Xcode中獲取錯誤

ViewController.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import "Player.h" 


@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, 
CLLocationManagerDelegate,NSXMLParserDelegate, UISearchBarDelegate> { 
    IBOutlet UISearchBar *searchBar; 

    CLLocationManager *locationManager; 

    NSMutableArray *coolarray; 

    float latitude; 
    float longitude; 
} 
@property(nonatomic,strong) IBOutlet UITableView * tableView; 
@property (nonatomic, retain) CLLocationManager *locationManager; 

@end 

ViewController.m

#import "ViewController.h" 
#import "LoadingViewController.h" 


@interface ViewController() 

@end 

@implementation ViewController 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (void)dealloc 
{ 
    [super dealloc]; 
    [self.locationManager release]; 
    if (coolarray) 
     [coolarray release]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    coolarray = NULL; 

    self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    self.locationManager.delegate = self; 
    [self.locationManager startUpdatingLocation]; 
    [[self navigationController] setNavigationBarHidden:YES]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

// Table data delegate 
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    if (coolarray != NULL) { 
     return [coolarray count]; 
    } 
    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     Player *cell = [_tableView dequeueReusableCellWithIdentifier:@"Player"]; 

    if (cell == nil) 
    { 
     cell = [[[Player alloc] initWithFrame:CGRectZero reuseIdentifier:@"Player"] autorelease]; 
    } 
    NSDictionary *itemAtIndex =(NSDictionary *)[coolarray objectAtIndex:indexPath.row]; 
    cell.nameLabel.text = [itemAtIndex objectForKey:@"name"]; 
    return cell; 
} 




// XML request and parsing 
- (void)updateLocation:(CLLocation *)newLocation { 
    if (coolarray) { 
     [coolarray release]; 
    } 
    coolarray = [[NSMutableArray alloc] init]; 

    if (newLocation) { 
     latitude = newLocation.coordinate.latitude; 
     longitude = newLocation.coordinate.longitude; 
    } 

    NSString *urlString = [NSString stringWithFormat:@"(censored)"]; 

    NSXMLParser *locationParser = [[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease]; 
    [locationParser setDelegate:self]; 
    [locationParser parse]; 


    [_tableView reloadData]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 

    if ([elementName isEqualToString:@"location"]) { 
     [coolarray addObject:[[NSDictionary alloc] initWithDictionary:attributeDict]]; 
    } 
} 

// GPS handling 
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    [self updateLocation:newLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
} 

// Search bar handling 
- (void)searchBarSearchButtonClicked:(UISearchBar *)sb { 
    [self updateLocation:NULL]; 
    [searchBar resignFirstResponder]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)sb 
{ 
    [searchBar resignFirstResponder]; 
} 

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


} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if([segue.identifier isEqualToString:@"showDetail"]) 
{ 
    NSIndexPath * indexPath=[self->_tableView indexPathForSelectedRow]; 
    LoadingViewController* destViewController= segue.destinationViewController; 
    destViewController.myProgLang=[coolarray objectAtIndex:indexPath.row]; 
    NSLog(@"indexPathrow:%d",indexPath.row); 
} 
} 

@end 

LoadingViewController.h(我的詳細視圖)

#import <UIKit/UIKit.h> 

@interface LoadingViewController : UIViewController 
{ 


} 

@property (nonatomic, retain) IBOutlet UILabel *myLabel; 
@property (nonatomic, retain) NSString *myProgLang; 

@end 

LoadingViewController.m(我的詳細視圖)

#import "LoadingViewController.h" 

@interface LoadingViewController() 

@end 

@implementation LoadingViewController 
@synthesize myLabel, myProgLang; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    myLabel.text = myProgLang; 
    [[self navigationController] setNavigationBarHidden:YES]; 
} 

- (void)dealloc { 
    [myLabel release]; 
    [myProgLang release]; 
    [super dealloc]; 
} 
@end 

回答

0

問題是您的數組看起來是字典對象的集合。當你要繼續時,你正在從對象中取出字典,並試圖將它作爲一個NSString屬性來存儲。

試着改變你的prepareForSegue:方法如下:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if([segue.identifier isEqualToString:@"showDetail"]) 
    { 
     NSIndexPath * indexPath=[self->_tableView indexPathForSelectedRow]; 
     LoadingViewController* destViewController= segue.destinationViewController; 
     destViewController.myProgLang=[[coolarray objectAtIndex:indexPath.row] objectForKey:@"name"]; 
     NSLog(@"indexPathrow:%d",indexPath.row); 
    } 

} 
+0

再次感謝!這是你爲我解決的#4。我真的不能夠感謝你! – user1776234

+0

沒問題;很高興它的工作! :) –