Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

iphone - Can you add a UITableViewController's TableView to another View?

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.

enter image description here

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

  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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

  2. Add a UITableView in the XIB.

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

  4. 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

@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

That should do it!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...