Search code examples
iosuitableviewtableviewprogrammatically-created

Programmatically change Size of Table


The Storyboard: http://s7.directupload.net/images/140717/z5hwmezv.png

Hey guys, Ive got an app that recursively triggers a the same tableview controller (lets say there are files and folders in it) until you trigger a file instead of a folder. When a file is clicked, it jumps into the GLKit View Controller.

Now I want to resize the tableView programmatically, which wont work. I already got the window size, which I'm going to use for calculation the position and size of the tableView:

CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

I tried different ways to change the size like the following, which dont change anything.

mainTableView.frame = CGRectMake(0, 0, screenWidth, screenHeight);

It works If I programmatically create the mainTableView, but then my segue gets deleted and I did not found any solution to create a segue programmatically.

It would be great if you can help me to find a solution that works with a storyboard tableView.


Solution

  • 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

    //-- For table sections
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       return 1;
    }
    
    //-- 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
    }