2013-02-21 65 views
1

我在ViewController上有一個按鈕來加載TableViewController。按鈕後面的代碼是:加載TableViewController時出現NSInvalidArgumentException

- (IBAction)loadSearchView:(id)sender { 

SearchTableViewController *searchVC = [[SearchTableViewController alloc] initWithStyle:UITableViewStylePlain]; 
[[self navigationController] pushViewController:searchVC animated:YES]; } 

TableViewController負載,應用程序崩潰,出現以下錯誤:

2013-02-21 15:22:19.460 MyMusicLibrary[4159:c07] -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0x7aa0800 2013-02-21 15:22:19.462 MyMusicLibrary[4159:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0x7aa0800' * First throw call stack: (0x14b5022 0xeb5cd6 0x14b6cbd 0x141bed0 0x141bcb2 0x3279 0xb2c54 0xb33ce 0x9ecbd 0xad6f1 0x56d21 0x14b6e42 0x1d86679 0x1d90579 0x1d154f7 0x1d173f6 0x1d16ad0 0x148999e 0x1420640 0x13ec4c6 0x13ebd84 0x13ebc9b 0x139e7d8 0x139e88a 0x18626 0x1fdd 0x1f05) terminate called throwing an exception

它試圖在TableViewController的負荷下運行的代碼是:

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

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

    [[cell textLabel] setText:[NSString stringWithFormat:@"Cell %i", [indexPath row]]]; 

    return cell; 
} 

的numberOfSectionsInTableView和numberOfRowsInSection設置如下:

- (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 10; 
} 

但是,當它們都設置爲0時,將加載表視圖。誰能幫忙? :)

+0

你有沒有設置數據源和委託正確? – apascual 2013-02-21 16:02:57

+0

您使用的是哪個版本的iOS?這是iOS 6以來的有效方法。 – rdelmar 2013-02-21 16:13:59

回答

1

使用僅僅只有

dequeueReusableCellWithIdentifier:@"Cellidentifier" 

這樣

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cellidentifier"]; 
+0

謝謝了!所以新到iOS ... :) – Cheese1223 2013-02-21 16:09:52

+0

[tableView dequeueReusableCellWithIdentifier:forIndexPath:]僅適用於iOS 6.0及更高版本。 – nkongara 2013-02-21 16:28:27

-1

沒有可用- (id)dequeueReusableCellWithIdentifier: forIndexPath:的tableview方法,你只需要通過只標識符

- (id)dequeueReusableCellWithIdentifier: 
+1

是的,實際上有這樣一種方法。 – rdelmar 2013-02-21 16:11:46

+0

是的,它提供在iOS 6.0及更高版本 – nkongara 2013-02-21 16:23:02

+0

是的。你沒有在你的文檔中看到它嗎?如果不是,那麼你需要更新你的文檔集。 – rdelmar 2013-02-21 16:25:03