简体   繁体   中英

Creating A TableVIew Programmatically With Objective-C iOS

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!

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

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM