Objective c 创建单元格时发送到实例的选择器无法识别

Objective c 创建单元格时发送到实例的选择器无法识别,objective-c,cocoa-touch,uitableview,unrecognized-selector,Objective C,Cocoa Touch,Uitableview,Unrecognized Selector,我正在尝试重用在我的故事板中创建的单元 我正在使用以下代码: MyTableView * mytableview; UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; mytableview = [sb instantiateViewControllerWithIdentifier:@"myTable"]; AlertCell *cell; cell = [mytablevi

我正在尝试重用在我的故事板中创建的单元

我正在使用以下代码:

MyTableView * mytableview;

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
mytableview = [sb instantiateViewControllerWithIdentifier:@"myTable"];


AlertCell *cell;

cell = [mytableview.tableView dequeueReusableCellWithIdentifier:@"alertCell"];
cell.message.text = @"some text";

return cell;
我得到这个错误:

[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0xe02b7d0
这一行最终生成错误:

[mytableview.tableView dequeueReusableCellWithIdentifier:@"alertCell"];
  • 使用单元格时的表视图是通过编程创建的
  • 我想要的一切都管用!但我还是在日志中找到了错误

非常感谢

创建单元格时无法创建表视图

您必须在创建视图控制器时调用的方法中创建表视图,例如
viewDidLoad

UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
tableView.rowHeight = 45;
[self.view addSubview:tableView];
tableView.delegate = self;
tableView.datasource = self;
然后在cell方法中使用方法中包含的
tableView
指针

此外,该错误实际上也表明数据源存在问题


有这么多明显的知识差距,也许最好的办法是通读。

当您创建表时,我没有完全理解。您不应该同时实例化。你为什么要打这个电话

mytableview = [sb instantiateViewControllerWithIdentifier:@"myTable"];
如果要使用tableview,只需正常创建它并将其添加到viewcontroller中,而不是在
viewdiload
中使用上面的代码行

UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
tableView.rowHeight = 45;
[self.view addSubview:tableView];
tableView.delegate = self;
tableView.datasource = self;
实现tableview的数据源和委托方法。并在
单元格中单击rowatinexpath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newFriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newFriendCell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
//do your stuff
return cell;
}

我不确定您看到的错误是否在此代码中
objectforkeydsubscript
NSDictionary
上的一种方法,常用于订阅引用(即myDict[@“key]”)
NSArray
有一个类似的方法
objectAtIndexedSubscript
(myArray[3])。是否确定此错误不是由于将数组分配给需要字典的对象而导致的?可以在创建单元格时创建表,但不应该这样做。在我看来,OP正在加载其他视图控制器和其他表,只是为了创建一个新的单元格,这看起来效率很低。