我正在尝试创建一个TableView并输入来自NSArray的数据。然而,我得到了这个错误:
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我真的不确定是什么代码导致了这种情况,所以我只附加了两个table方法:
- (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;
}创建NSArray:
@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];
}发布于 2012-12-21 23:06:15
您没有正确设置表视图的数据源。这会使tableView:numberOfRowsInSection消息被发送到错误的对象,即错误消息中显示的UIView:
[UIView tableView:numberOfRowsInSection:]查看/发布设置UITableView的dataSource属性的代码,以找出错误所在。您可能传入了错误的对象。
数据源就是您根据您的帖子在其类中定义- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section的对象。
发布于 2012-12-21 23:12:22
在.h文件中使用以下代码
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *your_Table;在.m文件中
@implementation ViewController
@synthesize your_Table;在Xib文件中,将Delegate和dataSource连接到Fileowner。
或者在ViewDidLoad中以编程方式分配它。
your_Table.delegate=self;
your_table.dataSuorce=self;在那之后,使用这个
- (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}希望能对你有所帮助。
如果没有,那么从.h文件中发布你的代码。
https://stackoverflow.com/questions/13992338
复制相似问题