ios - 在 UIView 中放置 UITableView 时,永远不会命中 cellForRowAtIndexPath

标签 ios uitableview uiview ios7

我已经进行了一些广泛的研究,并且知道使用普通 UITableViewController 时的陷阱。 .我还发现了类似的 questions没有任何运气就已经回答了。我一定错过了一些非常微不足道的东西。

我已经上传了test project在 Github 上。

我的目标是将 UITableView 放在 UIView 中。

enter image description here

我有一个 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 )。当然,要查看表格中的任何内容,您需要在单元格中添加一些内容。

编辑后:

enter image description here

右键单击文件所有者以显示黑色窗口。正如您从图像中看到的,“ View ”连接到 TableView 。单击该行 TableView 旁边的“x”以删除该连接,然后从右侧的开放圆圈拖动到显示“ View ”的位置(就在 First Responder 立方体下方)。

关于ios - 在 UIView 中放置 UITableView 时,永远不会命中 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055201/

相关文章:

ios - NSFetchedResultsController 看不到插入(或更新)

objective-c - 如何使 UIView 子类框架在外部不可变

ios - MapKit 和自定义类

ios - 如何更改Tabbar中图标的颜色? ( react native 导航)

ios - 是否可以从 IOS sdk 中的 Dropbox 和 Google Drive 文件应用程序获取文件

ios - 正确的 loadView 实现

ios - 滚动文本 - 自定义动画

c# - Xamarin 仅在设备上丢失了自定义对象类别(发送到实例的无法识别的选择器)

ios - 如何使单元格重叠?

ios - 显示数据库中的数据 - CoreData 如何走?