Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to add Static-TableView-Cells to a UIViewcontroller?

I want to add a tableview-look-a-like-login to my app, but it seems to be not that easy to implement. I tried to accomplish my goal using more then one approach, but i am not sure about which solution is the best.

For example, Dropbox and Facebook have a login page like this.

Here are my 3 approaches :

  1. I added 2 UITextfields to my View (no border) and placed a . png behind, which looks like a tableviewcell. ( Not the best approach cause i want to use real tableviews )

  2. I added a Container View to my ViewController placed a tableview with static Table Views inside. The Problem here is, that i dont know how to access the information inside my viewcontroller?

  3. I added a tableview to my ViewController and used dynamic cells with it. Connected the outlets for delegate and datasource to my viewcontroller and initialized them with the delegate and datasource methods. The Problem here is, that i can not use static table views inside a uiviewcontroller.

Is there any better way of solving this problem ? I would really like to know how to do this in a more elegant way.

EDIT:

A ContainerViewController basically solved this issue for me some month ago. After embedding one into the main controller you can access it through the prepareForSegue function and define a protocol-based interface for that specific controller to interact with the embedded controller.

like image 900
Sebastian Boldt Avatar asked Dec 12 '12 19:12

Sebastian Boldt


2 Answers

If you want to use static cells inside a regular UIViewController, just add the static cells and design them the way you like in interface builder, then connect the table cells as strong IB outlets (weak won't work, make sure they are strongly referenced). This will work flawlessly if you have a few table cells. Then set the view controller as the data source of the tablet view, implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return the number of cells and implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to return your strongly referenced cell instance for the specified index path. I've used this method for a simple table view in my view controller that had four cells and it is working perfectly. For a large-dynamic data set, I definitely do not recommend this approach but for small, static tables, this does the job right.

like image 89
Can Poyrazoğlu Avatar answered Nov 14 '22 22:11

Can Poyrazoğlu


I have an idea how to solve this. I think it's a clean way to do so. You do not need storyboard for this controller.

Make your controller subclass UITableViewController like so:

@interface YourViewController : UITableViewController

Then in your viewDidLoad you create the instances of the cells:

- (void) viewDidLoad {
    usernameCell = [YourTextFieldCell new];
    passwordCell = [YourTextFieldCell new];
}

The YourTextFieldCell is of course your own subclass of a UITableViewCell, which could be something like this:

@implementation YourTextFieldCell {
    UITextField textField;
}
- (id) init {
    self = [super init];
    if (self) {
        // Adjust the text's frame field to your liking
        textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
        [self addSubview:textField];
    }
}
// A getter method to access the textfield from the outside
- (UITextField *) textField {
    return textField;
}

@end

Back in YourViewController:

- (NSInteger) tableView:(UITableView *) tv numberOfRowsInSection:(NSInteger) section {
    return 2;
}
- (UITableViewCell *) tableView:(UITableView *) tv cellForRowAtIndexPath:(NSIndexPath *) indexPath {
    if (indexPath.row == 0) {
        return usernameCell;
    } else if (indexPath.row == 1) {
        return passwordCell;
    }
    return nil;
}

Do you get where I am going with this? This is how I think you should do it! Good luck!

like image 28
Tom van Zummeren Avatar answered Nov 14 '22 20:11

Tom van Zummeren