簡體   English   中英

加載TableViewController時出現NSInvalidArgumentException

[英]NSInvalidArgumentException when loading a TableViewController

我在ViewController上有一個按鈕可以加載TableViewController 該按鈕后面的代碼是:

- (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:]:無法識別的選擇器已發送到實例0x7aa0800 2013-02-21 15:22:19.462 MyMusicLibrary [4159:c07] *正在終止應用由於未捕獲的異常'NSInvalidArgumentException',原因是: ' - [UITableView的dequeueReusableCellWithIdentifier:forIndexPath:]:無法識別的選擇發送到實例0x7aa0800' *第一擲調用堆棧:(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)終止調用引發異常

它試圖在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時,將加載表視圖。 有人可以幫忙嗎? :)

僅使用

 dequeueReusableCellWithIdentifier:@"Cellidentifier" 

像這樣

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

沒有可用的tableview方法- (id)dequeueReusableCellWithIdentifier: forIndexPath:您只需要傳遞標識符即可

- (id)dequeueReusableCellWithIdentifier:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM