我已经搜索了互联网,并没有得到任何结果。我的 ios 应用程序只是一个带有 indexpath 的简单导航控制器,它应该将自定义动态单元格 () 选择的行的标题发送到 uilabel。问题是数据源来自mysql,它是在nsarray(称为coolarray)中解析的xml。该错误发生在移动到下一个视图之前,并且在日志中为:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“[__NSDictionaryI isEqualToString:]:无法识别的选择器已发送到实例 0x8887670”。但是,它确实给出了 indexPathrow:0 或 1 或任何选择的 nslog 结果。一定是数组传递到uilabel有问题,但我想不通!我已经省略了自定义单元格的代码,因为这不太可能是错误的原因并且工作正常(加号很简单)。这是我之前在UITableView 拒绝进入 detailViewController的问题的后续,但该代码不再相关,因为如下所示,我的代码发生了很大变化。
视图控制器.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
视图控制器.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