Iphone 如何以编程方式将现有UITableView分配/替换为新UITableView?
我的UIViewController类实现有以下代码:Iphone 如何以编程方式将现有UITableView分配/替换为新UITableView?,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,我的UIViewController类实现有以下代码: - (void)viewDidLoad { CustomUITableView *customUITableView = [[CustomUITableView alloc] initWithFrame:self.view.frame]; [self.view addSubview:customUITableView]; [super viewDidLoad]; } 当我运行应用程序时,表格
- (void)viewDidLoad {
CustomUITableView *customUITableView = [[CustomUITableView alloc] initWithFrame:self.view.frame];
[self.view addSubview:customUITableView];
[super viewDidLoad];
}
当我运行应用程序时,表格显示得很好。这种方法有两个问题:
有人知道这是否可能吗?示例代码将非常有用。谢谢。您可以从interface builder加载常规UITableView,然后使用UITableView中感兴趣的参数初始化CustomUITableView
- (void)viewDidLoad {
//Your tableView was already loaded
CustomUITableView *customUITableView = [[CustomUITableView alloc] initWithFrame:tableView.frame style:tableView.style];
customUITableView.backgroundColor = tableView.backgroundColor;
//...
//... And any other properties you are interested in keeping
//...
[self.view addSubview:customUITableView];
[super viewDidLoad];
}
如果确保指定您认为将在interface builder中修改的每个属性,则此方法将起作用。只需确保在复制属性后删除通过nib加载的UITableView即可。确保可以替换该表。 简单到
Class MyTableClass = NSClassFromString(@"WhateverClass");
id newTableView = [[MyTableClass alloc] init];
[newTableView performSelector:@selector(someSel:)];
确保首先从原始nib实例化视图设置数据源和委托以及任何属性,然后
然后,您可以将本地tableview属性设置为这个新对象,只要ivar具有适当的强制转换,您就可以对其调用方法,而无需像上面那样调用运行时
如果您想以不同的方式进行,还可以深入研究obj-c运行时。
你说的故事板是什么意思?你可以在这里读到:哦,好的!硬编码怎么样?我不明白。将“it”硬编码为“what”?我也在想同样的事情,但希望更简单:)。这是我的后备计划。唐克斯·乔尔。