Creating A TableVIew Programmatically With Objective-C iOS

Go To StackoverFlow.com

13

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!

2012-04-03 23:41
by The Man


20

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
2012-04-04 06:08
by demon9733


16

Step 1: Add delegate UITableViewDataSource,UITableViewDelegate

@interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate>
{
   UITableView *tableView;
}

Step 2:

-(void)viewDidLoad
{
    tableView=[[UITableView alloc]init];
    tableView.frame = CGRectMake(10,30,320,400);
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [tableView reloadData];
    [self.view addSubview:tableView];
}

Step 3: Properties for tableview (rows & column)

//-- For no of rows in table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//-- Table header height if needed

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 50;
}

//-- Assign data to cells

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

   if (cell == nil)
   {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
   cell.textLabel.text=[your_array objectAtIndex:indexPath.row]; ***(or)*** cell.textLabel.text = @"Hello";
   return cell;
}

//-- Operation when touch cells

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Your custom operation
}
2014-02-04 05:13
by svmrajesh


4

Instead of setting the view of the UIViewController, add the tableView as a subview.

Instead of:

self.view = tableView;

Do this:

[self.view addSubview:tableView];

This will properly respect the frame that you set.

2012-04-03 23:43
by DHamrick
I did that, but now nothing happens. --- Sorry, I now get an error - The Man 2012-04-03 23:45
Could be that you mispelled tabrleView. It should be tableView everywhere - DHamrick 2012-04-03 23:55
Yea I did that because I had another TableVie - The Man 2012-04-03 23:57
@DHamrick is right. remove any line where you assign to the view. for any table you are using, however it's spelled, do [self.view addSubview:...]; you might also be tripping up on the autoresizingMask. Get it working first by commenting that out - danh 2012-04-04 00:00
I did what both of you said, and I get this.. - The Man 2012-04-04 00:03
* Assertion failure in -[UITableView createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKitSim/UIKit-1914.84/UITableView.m:6061 2012-04-03 20:02:44.415 Dot Golf Scoring[728:f803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: - The Man 2012-04-04 00:04