Creating A TableVIew Programmatically With Objective-C iOS

ios, iphone, objective-c, xcode

Solution

When you set `dataSource` and `delegate` properties for your `UITableView`, it means, you have to write at least this methods for `dataSource`:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

If you wouldn't do this, it will be crash. Summary you'll get this (this code may contain syntax or logic errors - I wrote it in notepad):

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    UITableView *firstTableView;
    UITableView *secondTableView;
}

@end

//

@implementation YourViewController

#pragma mark - Objects Processing

- (void)addGolfer:(UIBarButtonItem *)sender {
    if (secondTableView) {
        [secondTableView removeFromSuperView];
        secondTableView = nil;
    }

    secondTableView = [[UITableView alloc] initWithFrame:CGRectMake(101, 45, 100, 416)];
    secondTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    secondTableView.delegate = self;
    tabrleView.dataSource = self;

    [self.view addSubview:secondTableView];
}

#pragma mark - TableView DataSource Implementation

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == firstTableView) { // your tableView you had before
        return 20; // or other number, that you want
    }
    else if (tableView == secondTableView) {
        return 15; // or other number, that you want
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    cell.backgroundView = [[UIView alloc] init];
    [cell.backgroundView setBackgroundColor:[UIColor clearColor]];
    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    if (tableView == firstTableView) { // your tableView you had before
        // ...
    }
    else if (tableView == secondTableView) {
        cell.titleLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row + 1];
    }

    return cell;
}

@end

Problem

I'm new to developing iOS applications and Objective C itself, so I have a probably very simple question. Currently I have the following method that is called from a ToolBar Button click. The method is designed to create a table view in the frame variable fr. ``` - (IBAction)addGolfer:(id)sender { CGRect fr = CGRectMake(101, 45, 100, 416); UITableView *tabrleView = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain]; tabrleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; tabrleView.delegate = self; tabrleView.dataSource = self; [tabrleView reloadData]; self.view = tableView; } ``` The result of calling this method is not what I expect. Instead of creating the Table View in the frame "fr", the table view fills the entire screen. Again I'm totally new and would a appreciate any answers and any suggestions. Thanks!

Original source