滚动或点击时UITableView崩溃 - ARC问题?

时间:2012-04-18 19:43:30

标签: uitableview automatic-ref-counting

我正在尝试使用ARC在项目中创建一个简单的UItableview应用程序。该表渲染得很好,但如果我尝试滚动或点击一个单元格,应用程序崩溃。

看看NSZombies(这是正确的说法吗?)我收到消息“ - [PlacesViewController respondsToSelector:]:消息发送到解除分配的实例0x7c29240”

我认为这与ARC有关,因为我过去成功实现了UItableviews,但这是我第一个使用ARC的项目。我知道我一定很遗憾。

PlacesTableViewController.h

@interface PlacesViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;

@end 

PlacesTableViewController.m

#import "PlacesTableViewController.h"

@implementation PlacesViewController

@synthesize myTableView;
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.myTableView    =   [[UITableView alloc] initWithFrame:self.view.bounds   style:UITableViewStylePlain];

    self.myTableView.dataSource =   self;
    self.myTableView.delegate   =   self;

    [self.view addSubview:self.myTableView];
}
#pragma mark - UIViewTable DataSource methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    UITableViewCell *result = nil;

    static NSString *CellIdentifier = @"MyTableViewCellId";

    result =    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(result == nil)
    {
        result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    result.textLabel.text   =   [NSString stringWithFormat:@"Cell %ld",(long)indexPath.row];


    return result;
}
@end

1 个答案:

答案 0 :(得分:1)

您发布的代码没有明显错误。问题在于创建并保留在PlacesViewController上的代码。您可能正在创建它,但不能永久存储它。您的PlacesViewController需要保存到ivar中或放入一个视图容器中,以便为您管理它(UINavigationController,UITabController或类似的)

相关问题