簡體   English   中英

tableview無法正常工作 - [UIViewController tableView:numberOfRowsInSection:]:無法識別的選擇器發送到實例

[英]tableview not working - [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

我正在嘗試使用xib初始化uitableview但是當我在模擬器中運行應用程序時,拋出以下異常。

2013-06-16 10:40:48.552 CoreDataExample[60661:c07] -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81765a0
2013-06-16 10:40:48.554 CoreDataExample[60661:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81765a0'

在我嘗試啟動tableview之后的步驟下面:

  1. 在我的viewController添加UITableViewDelegateUITableViewDataSource
  2. tableview插入我的viewController.xib的視圖。
  3. 在文件的所有者中創建datasourcedelegate (按下控制鍵並從組件到文件的所有者拖動鼠標箭頭,然后選擇選項委托)。
  4. 實現方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath在我的viewController.m

下面是兩種方法的實現:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = nil;

    static NSString *identifier = @"identifier";

    cell = [tableView dequeueReusableCellWithIdentifier:identifier];

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

    cell.textLabel.text = @"Olá";
    cell.detailTextLabel.text = @"Subtitle" ;
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];


    return cell;
}

ViewController.h如下:

@interface CDEMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

@end

在XIB中,單擊文件所有者,檢查類類型。 我想這是UIViewController。 如果是UIViewController則將其更改為CDEMainViewController 它會正常工作。

在ViewController XIB中,右鍵單擊Tableview,檢查tableview數據源和委托可以與視圖連接,而不是與文件所有者連接,斷開這些並再次連接tableview數據源並委托文件所有者,這解決了我的問題。 請檢查屏幕截圖以獲得參考。

InCorrect Image

在此輸入圖像描述

正確的形象

在此輸入圖像描述

你有設定方法嗎?:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

嘗試打開xib文件並重新連接tableView的IBOutlet。 希望對你有所幫助。

暫無
暫無

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

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