我的UITableViewController的实现怎么错了?

时间:2012-03-12 03:46:09

标签: iphone objective-c xcode uitableview

此代码复制到其他任何地方似乎都有效。它只是在我的应用程序内部崩溃。有什么想法吗?

另一个.m ......

#import "JEntryTableViewController.h"
@interface JCreateViewController () {

    JEntryTableViewController *_tableView;

}

@property (nonatomic, strong) JEntryTableViewController *tableView;

@end

@implementation JCreateViewController

@synthesize tableView = _tableView;

- (id)init
{
    self = [super init];
    if (self) {

        self.tableView = [[JEntryTableViewController alloc] initWithStyle:UITableViewStylePlain];
        [self.view addSubview:self.tableView.view];

    }
    return self;
}

JEntryTableViewController.h:

#import <UIKit/UIKit.h>


@interface JEntryTableViewController : UITableViewController {

}

@end

JEntryTableViewController.m:

#import "JEntryTableViewController.h"

@interface JEntryTableViewController ()

@end

@implementation JEntryTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

#pragma mark - Table view data source

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CountryCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    return cell;
}

#pragma mark - Table view delegate

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

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end

我把它作为一个快速测试来确保它设置正确,令我惊讶的是,当我滚动回到我已经看过的单元格时,它崩溃了并且给了我一个EXC_BAD_ACCESS错误。不幸的是,调试区域没有给我任何可以使用的东西,我真的不知道问题是什么 - 它是如此基本,简单的一堆代码。我不知道该解决什么问题。它应该工作。

2 个答案:

答案 0 :(得分:1)

实现tableView的方法可能不像我们常用的那样。

您可以将tableView直接添加到ViewController中,而无需使用从UITableViewController继承的另一个viewController。

您应该做的与您在JEntryTableViewController中所做的完全相同。

当遇到EXC_BAD_ACCESS问题时,有几种解决方案可以找到确切的问题。 1. EXC_BAD_ACCESS signal received http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/

  1. 在Xcode break points的右侧部分,您可以添加这些断点,它可以帮助您快速找到异常情况。

答案 1 :(得分:0)

两件事:

  1. 写入<UITableViewDataSource, UITableViewDelegate> JEntryTableViewController.h档案
  2. 在此处记下崩溃日志,以便我们轻松解决您的问题。