2013-02-10 47 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