我已经进行了一些广泛的研究,并且知道使用普通 UITableViewController
时的陷阱。 .我还发现了类似的 questions没有任何运气就已经回答了。我一定错过了一些非常微不足道的东西。
我已经上传了test project在 Github 上。
我的目标是将 UITableView 放在 UIView 中。
我有一个 xibfile 并将文件的所有者设置为我的 View Controller ,并将所有内容连接起来。
@interface TNViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
这就是 View Controller 的实现的样子。
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:@"TNViewController" bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (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];
}
return cell;
}
在 App 委托(delegate)中,我像这样运行它:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
TNViewController *vc = [TNViewController new];
[[self window] setRootViewController:vc];
...
}
然而,表格仍然是空的,
cellForRowAtIndexPath
永远不会被击中。它必须与包装表格的 UIView 有关,但我不确定我错误地连接了什么。
最佳答案
查看您的测试项目,您犯了将文件所有者的 myTable 和 View 导出连接到 TableView 的错误。 View 导出应该连接到主视图(它更容易连接到对象列表中的 View 而不是 Canvas 上的 View )。当然,要查看表格中的任何内容,您需要在单元格中添加一些内容。
编辑后:
右键单击文件所有者以显示黑色窗口。正如您从图像中看到的,“ View ”连接到 TableView 。单击该行 TableView 旁边的“x”以删除该连接,然后从右侧的开放圆圈拖动到显示“ View ”的位置(就在 First Responder 立方体下方)。
关于ios - 在 UIView 中放置 UITableView 时,永远不会命中 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055201/