2012-12-21 79 views
0

我試圖創建一個TableView中,把在從NSArray中的數據。不過,我收到此錯誤:的TableView - 信號SIGABRT

2012-12-21 15:53:10.239 Arr[13219:c07] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x71ccb60 
2012-12-21 15:53:10.240 Arr[13219:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x71ccb60' 
*** First throw call stack: 
(0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0x1f96e8 0x1fc3c4 0xc0fa2 0xc092c 0xc4426 0xc90ce 0x6592d 0x10e16b0 0x228cfc0 0x228133c 0x228ceaf 0x1048cd 0x4d1a6 0x4bcbf 0x4bbd9 0x4ae34 0x4ac6e 0x4ba29 0x4e922 0xf8fec 0x45bc4 0x45dbf 0x45f55 0x4ef67 0x12fcc 0x13fab 0x25315 0x2624b 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x137da 0x1565c 0x1c2d 0x1b55 0x1) 
libc++abi.dylib: terminate called throwing an exception 

我真的不知道什麼代碼是導致此,所以我就附​​上兩個表方法:NSArray中的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

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

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    return cell; 
} 

創建:

@implementation ArrViewController { 
    NSArray *tableData; 
} 

@synthesize itemTxt = _itemTxt; 
@synthesize itemsLabel = _itemsLabel; 
@synthesize model = _model; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil]; 
} 
+2

你有' - (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView'方法在你的代碼? –

+1

不,我不這樣做,而是根據[AppCoda(http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/),它的工作原理沒有它 –

+0

是如果它不是一個分組tableview你可能不需要它,否則'返回整數' –

回答

3

您無法正確設置表格視圖的數據源。這使得被髮送到了錯誤的對象tableView:numberOfRowsInSection消息,UIView,因爲它從出現錯誤消息:

[UIView tableView:numberOfRowsInSection:] 

評論/張貼在那裏你設置你UITableViewdataSource屬性來找出什麼是錯的代碼。你很可能傳遞了錯誤的對象。

的數據源是在非常對象的類定義- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section按您的文章。

+0

我只是使用圖形用戶界面生成器來CTRL-拖動TableView到ViewController。我已經將NSArray對象添加到線程 –

+0

您在哪裏定義'tableView numberOfRowsInSection'?那就是要實例化爲一個數據源的類...你能說明你對SimpleTableController做了什麼,以及如何實例化表視圖? – sergio

0
在您的.h文件中

使用下面的代碼

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 

@property (weak, nonatomic) IBOutlet UITableView *your_Table; 

在你的.m文件

@implementation ViewController 
@synthesize your_Table; 

Xib文件連接DelegatedataSourceFileowner

ViewDidLoad它分配編程

your_Table.delegate=self; 
your_table.dataSuorce=self; 

使用此

- (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section { 
    return [tableData count]; 
} 

後希望它會幫助你。

如果沒有,那麼從後.h文件中您的代碼。