【问题标题】:Load UIView with a tableview in it加载 UIView,其中包含一个 tableview
【发布时间】:2014-06-04 14:27:04
【问题描述】:

我试图在我的 ViewController 上加载一个自定义 UIView ,这个 UIView 加载一个 xib 文件并有一个 tableView ,所以当我将 dataSourcedelegate 与其文件的所有者连接时,当我尝试午餐应用程序时,由于此消息而崩溃:

 TABLE VIEW LOAD  
 -[DinoViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to
instance 0x993b070  *** Terminating app due to uncaught exception

 'NSInvalidArgumentException', reason: 
'-[DinoViewController     tableView:numberOfRowsInSection:]: unrecognized selector sent to
 instance 0x993b070'
 *** First throw call stack:

这是我的代码:

//DinoViewController

- (IBAction)searchDino:(id)sender {

        DinoTable* tableview = [[DinoTable alloc]initWithFrame:CGRectMake(0,0,384,1024)];
        NSArray * dinoTableView = [[NSBundle mainBundle] loadNibNamed:@"DinoTable" owner:self options:nil];
        tableview = dinoTableView[0];
        [self.view addSubview:tableview];
    }

.TableView:

//  DinoTable.h
@interface DinoTable : UIView <UITableViewDataSource , UITableViewDelegate>  {

    NSArray *list;
}

//  DinoTable.m



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {


        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"dinoName" ofType:@"plist"] ;
        list = [NSArray arrayWithContentsOfFile:filePath];
        NSLog(@"TABLE VIEW LOAD");


    }
    return self;
}




 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {



 }


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}


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

    return list.count;
}




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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [list objectAtIndex:indexPath.row];

    return cell;
}

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

从代码来看,DinoTable 是一个UIView,没有UITableView。我猜您根据错误将datasourcedelegate 指向DinoTable。这些应该指向UITableView

DinoTable 内添加UITableView,并添加您的类可以用来设置委托/数据源的方法。

【讨论】:

  • 我在 UIView 的 xib 文件中添加了 UITableView,顺便添加了这些代码:[_tableView setDataSource:self]; [_tableView setDelegate:self]; 但没有任何改变!
  • 尝试在 .h 文件中设置委托 像这个例子stackoverflow.com/questions/11265039/…
  • @Mc.Lover 你能发布你的 DinoViewController 代码吗?我注意到错误来自那里。您将tableView 设置为DinoView。尝试从 XIB 取消链接数据源和委托。我认为您仍然让它们指向 UIView。
  • 我将datasourcedelegatefile's owner 取消链接它有效,但是如何在tableview 上显示数据?! DinoViewController 中没有代码,只是一个打开自定义 UIView 的按钮,检查这行代码:- (IBAction)searchDino:(id)sender
  • 那是因为您将数据源和委托链接到UIViewController,而不是UITableView 所在的位置。查看此评论末尾的链接(第一个答案)。将setUpTableView 添加到DinoTable,然后在实例化DinoTable *tableview 后立即在DinoViewController 中调用[tableView setUpTableView]stackoverflow.com/questions/18970013/…
猜你喜欢
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
相关资源
最近更新 更多