How to setup a static UITableView as a subview of

2019-02-06 15:19发布

When I work with a TableViewController I am able to setup all my content in storyboards. Since I use Static Cells instead of Dynamic Properties for my table view, I find this method much more convenient and easier to implement. I hook up the new UITableView class and simply delete all the delegate methods. Works like a charm as ALL of the content / buttons are being setup in storyboards.

I am trying to accomplish the same result, except this time, I have to work within a ViewController and add a TableView as a subview. Once I hook up the right class, add my outlet connection and setup the following delegates:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

This works well if my TableView is set to Dynamic Properties : enter image description here

But when I change the Table View content to Static Cells and delete the delegate method, my app crashes. So, How can I add a table view with Static Cells (That I can manipulate in storyboards) to my ViewController?

3条回答
老娘就宠你
2楼-- · 2019-02-06 15:24

As far as I know, you can't do this directly. At least in iOS 6, you had to use a UITableViewController when using static cells. One way to use a static table view inside a UIViewController would be to add a container view in IB, and make the embedded controller a table view controller (delete the UIViewController you get automatically, drag in a UITableViewController, and hook it up with the embed segue). You can get a reference to this table view controller from the UIViewController by implementing prepareForSegue:sender:, and using the destinationViewController property (which will point to the table view controller).

查看更多
再贱就再见
3楼-- · 2019-02-06 15:33

You still need to do a couple of things:
Add <UITableViewDataSource, UITableViewDelegate> to your @interface declaration.
Then you can set these also in Interface Builder.
Implement cellForRowAtIndexPath and call the dequeueReusableCellWithIdentifier method to return the cell.

Sorry, I was wrong. The truth is you cannot use static cells without a UITableViewController. Sorry.

A solution could be that you create two controllers and just add the view of the table view controller to your other view controller.

查看更多
冷血范
4楼-- · 2019-02-06 15:40

Here is what you can do. In your storyboard, create a parent view controller that contains all your non tableview views also, create a UITableViewController. In your parent view controller, create container view, delete the view controller it auto adds, and right click and drag from the container view to your UITableViewController to create an embed segue. Your end result should look something like this:

enter image description here

查看更多
登录 后发表回答