你可以将UITableViewController的TableView添加到另一个View吗? [英] Can you add a UITableViewController's TableView to another View?

查看:452
本文介绍了你可以将UITableViewController的TableView添加到另一个View吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经插入了一个UITableViewController,它将相应的UITableView插入到一个简单的IB文档中。目标是将UITableView包含在父UIWindow(或UIView)中,其他东西(任何真正的东西)装饰在表中。以下是Interface Builder中的内容。

I've inserted a UITableViewController and it's corresponding UITableView into a simple IB document. The goal is to include the UITableView inside of a parent UIWindow (or UIView) with other "stuff" (anything really) adorning the table. Here's what that might look like in Interface Builder.

我已经尝试了很多次并且总是到达同一个地方。

I've tried this numerous times and always get to the same place.


  1. 构建一个充满数据的UITableViewController的工作子类

  2. 自定义UTableView及其单元格,包括点击目标

  3. 添加新的将UITableViewController创建为IB文档

  4. 将UITableView拖出UITableViewController并进入主UIView

  5. 将UITableViewController连接到UITableView

  6. 注意:在代码中添加UITableViewController导致同样的问题

  1. Build a working subclass of UITableViewController filled with data
  2. Customize the UTableView and it's cells including tap targets
  3. Add the newly created UITableViewController into an IB document
  4. Drag the UITableView out of the UITableViewController and into the main UIView
  5. Wire up the UITableViewController to the UITableView
  6. Note: adding the UITableViewController in code results in the same problem

在iPhone中运行应用程序时模拟器或在桌面上正确显示的设备上,但在您第一次尝试与其交互时崩溃。滚动,点按,任何东西都会崩溃应用程序。这似乎是一个委托问题,就像UITableView不知道如何与UITableViewController进行通信,但我不知道如何纠正这个问题。

When running the app in the iPhone emulator or on a device the table displays correctly, but crashes the first time you try and interact with it. A scroll, a tap, anything crashes the app. This seems to be a delegate problem, like the UITableView doesn't know how to communicate back to the UITableViewController, but I have no idea how to correct the problem.

所以我已经能够通过自定义tableHeaderView来获得足够的布局,但我真的更喜欢让其他技术工作。

So far I have been able to get by by customizing the tableHeaderView to get layouts that suffice, but I'd really prefer to have the other technique work.

推荐答案

你走在正确的轨道上!以下是您需要做的事情:

You're on the right track! Here's what you need to do:


  1. 创建一个标准的UIViewController子类及其附带的视图xib。

  1. Create a standard UIViewController subclass with its accompanying view xib.

在XIB中添加UITableView。

Add a UITableView in the XIB.

连接所有内容。视图控制器将是表的委托和数据源,因此您必须实现这两个协议。

Wire everything up. The view controller will be the delegate and the datasource for your table, so you must implement both protocols.

在您的实现文件中,添加所有必需的数据源和查看tableview所需的方法:

In your implementation file, add all of the necessary datasource and delegate methods needed for the tableview:

(UITableViewCell *)tableView:(UITableView *)tv
    cellForRowAtIndexPath:(NSIndexPath *)indexPath


您的头文件可能看起来类似这样的事情:

Your header file may look something like this:

MyViewController.h

MyViewController.h

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    IBOutlet UITableView *myTableView;
    //This outlet is only necessary if you need to send messages to the table view (such as reloadData)
}
@end

应该这样做!

这篇关于你可以将UITableViewController的TableView添加到另一个View吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆