2011-05-05 38 views
1

我有这个“tableView:cellForRowAtIndexPath::无法识别的选择器发送到实例”错误,我几乎肯定是某种内存管理问题,但现在我几乎厌倦了找到bug。 只要你不向下滚动,TableView就会显示单元格,如果你这样做的话。 应用程序崩溃。 我使用的唯一财产叫做“地方”,我已经检查过我是否没有错过“自我”。典型的UITableView无法识别的选择器发送到实例

所以..这里是我的代码:

#import "PlacesViewController.h" 
#import "FlickrFetcher.h" 
#import "SinglePlaceViewController.h" 

@implementation PlacesViewController 

@synthesize places; 

- (NSArray *)places 
{ 
    if (!places) { 
     NSSortDescriptor *content = [[NSSortDescriptor alloc] initWithKey:@"_content" ascending:YES]; 
     NSArray *unsortedPlaces = [FlickrFetcher topPlaces];  
     places = [unsortedPlaces sortedArrayUsingDescriptors:[NSArray arrayWithObjects: content, nil]]; 
    } 
    return places; 
} 

#pragma mark - 
#pragma mark Initialization 

#pragma mark - 
#pragma mark View lifecycle 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.title = @"Top Places"; 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 


// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return YES; 
} 


#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return self.places.count; 
} 

- (NSDictionary *)placeAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [self.places objectAtIndex:indexPath.row]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"PlacesTableViewCell"; 
    NSLog(@"%@", [FlickrFetcher topPlaces]); 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    NSString *location = [[self placeAtIndexPath:indexPath] objectForKey:@"_content"]; 
    NSArray *splitLocation = [location componentsSeparatedByString:@", "]; 
    cell.textLabel.text = [splitLocation objectAtIndex:0]; 
    if (splitLocation.count == 2) 
     cell.detailTextLabel.text = [splitLocation objectAtIndex:1]; 
    if (splitLocation.count == 3) 
     cell.detailTextLabel.text = [[[splitLocation objectAtIndex:1] stringByAppendingString:@","] stringByAppendingString:[splitLocation objectAtIndex:2]]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 


#pragma mark - 
#pragma mark Table view delegate 

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

    // Navigation logic may go here. Create and push another view controller. 

    SinglePlaceViewController *spvc = [[SinglePlaceViewController alloc] init]; 
    // Pass the selected object to the new view controller. 
    spvc.placeId = [[self placeAtIndexPath:indexPath] objectForKey:@"place_id"]; 
    [self.navigationController pushViewController:spvc animated:YES]; 
    [spvc release]; 

} 


#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [places release]; 
    [super dealloc]; 
} 


@end 

非常感谢你的帮助,我让你们所有的人真的很抱歉做了一些工作对我来说。

+1

你可以忽略整个'//配置单元格...'部分并滚动吗?我知道这不会显示文字,但它会崩溃吗?请发布堆栈跟踪和完整的控制台输出。 – 2011-05-05 07:23:03

+0

“PlacesViewController”继承了哪些类?你应该检查你的tableview的代表 – VietHung 2013-05-29 04:24:05

+0

你有没有尝试过在你的项目中设置一个异常断点?信息在哪里发送?接收者是谁? – Jarsen 2013-06-28 21:07:43

回答

0

您没有提供有关错误的很多信息,所以我只能猜测:

的错误表明得到消息cellForRowAtIndexPath:对象实际上没有这种方法来实现。 但是,既然你DID实现了它,我能想到的唯一原因就是你正在搞乱你的tableView的“dataSource”属性并将它改变为你不应该的东西。

确保dataSource是您的PlacecViewController

0

从它看起来对我来说,places属性的getter方法不会保留它返回的值。您将其设置为您从sortedArray方法获得的NSArray的自动发布实例。我敢打赌,它显示你的初始tableview单元格后被释放。

相关问题