2013-02-10 48 views
1

我只是按照教程中的几个步骤创建一个简单的50行TableView,但我得到“信号SIGABRT”:/ 我连接了Storyboard中的TableView和我创建的TableViewController类。信号SIGABRT简单表视图

这里是我的简单的代码:

#import "TableViewController.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 

    cell.textLabel.text = [NSString stringWithFormat:@"Row %i",indexPath.row]; 

    return cell; 
} 

回答

2

欢迎堆栈溢出!前一段时间,设置UITableViewCell的标准方法略有改变。这意味着模板代码Xcode提供了tableViews使用-tableView: dequeueReusableCellWithIdentifier: forIndexPath:方法,而年长的教程和书籍(大部分)使用tableView: dequeueReusableCellWithIdentifier:

如果你想这样做的新途径(-tableView: dequeueReusableCellWithIdentifier: forIndexPath),你需要以添加 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];viewDidLoad中,(或者在storyboard/nib中设置原型单元重用ID,并适当地设置单元类型 - 基本上应该为正常单元格做)。

旧的方式(tableView: dequeueReusableCellWithIdentifier:)通常跟一个if声明是这样的:

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

(其中\\Configure the cell. . .评论是)

虽然这是真的简单的东西,为公平起见,大多数教程做教老方法,如果您没有注意到两种方法之间的小差异,我想可能会让初学者感到困惑。展示新方法的教程是here