How to visually create and use static cells in a UITableView embedded in a UIViewController

痞子三分冷 提交于 2019-11-27 06:03:08

You are right. In storyboard, you cannot have a tableView with static cells embedded in a viewController. One way around it (I have not tried it myself, though, so I am not sure if it works) can be that you create an instance of UITableViewController in storyboard with static cells. Add an instance of UIView to your viewController, and then programmatically load the tableView of the UITableViewController into the UIView of your viewController.

You can achieve this in Xcode 4.5 and later versions, assuming your app is targeted at iOS 6+.

In the Storyboard simply create a UIViewController with a View Container inside it's main view. Then hook up that View Container to a UITableViewController that contains static cells.

Just like this:

You don't need a single line of code. Just control click, drag and select embed. The view controller containment is handled for you.

pmd's answer works but in the event that backward compatibility with iOS 5 is required as well, you can do the embedding programatically using the View Containment API.

In the viewDidLoad method of your parent UIViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    MyTableViewController* vc =[storyboard instantiateViewControllerWithIdentifier:@"MyTableVC"];
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];

    // ensure embedded view is aligned to top
    CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    vc.view.frame = frame;

    [vc didMoveToParentViewController:self]; 
}

Don't forget to specify a storyboard ID for your UITableViewController with the static cells.

I know this is an old question but I have a scrappy solution to this issue.

I needed 3 static cells on a UIViewController so this is what I did:

  1. Drag in some Table View Cells into your interface (not into UITableView) - add text and whatever else you need.
  2. Make IBOutlet properties for your cells and synthesise them.
  3. Drag a button and make it cover the entire cell. Set the button to type 'Custom' so it appears invisible - repeat for all cells
  4. Add numeric tags to your buttons
  5. Implement the following functions. buttonDown is connected to the buttons 'Touch Down' event. buttonUp is connected to 'Touch Up Inside' AND 'Touch Up Outside'

    -(IBAction)buttonDown:(id)sender {
        if ([sender tag] ==  1) {
            myFirstCell.selected = YES;
        }
        else if ([sender tag] ==  2) {
            mySecondCell.selected = YES;
        }
        else if ([sender tag] ==  3) {
            myThirdCell.selected = YES;
        }
    }
    
    -(IBAction)buttonUp:(id)sender {
        myFirstCell.selected = NO;
        mySecondCell.selected = NO;
        myThirdCell.selected = NO;
    }
    

You can do whatever else you like in the buttonDown event and use the button to go directly to a new view. I find this pretty useful.

I am not sure what you mean by static cells, but if you are trying to build the cells in IB, and then want to use it in your tableView, what you could do is in your cellForRowAtIndex you can call loadNibNamed passing the name of the .nib file you created for the cells as the parameter. Make sure that you have an outlet in your viewController which maps to the cell's .nib. Try exploring in these directions if that's what you are trying to achieve

You can make it dynamic and then switch of scrolling:

[yourTableName setScrollEnabled:NO];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!