1

In my main view I have a header (label) and footer (button). In the middle there is a table. Table takes data from MutableArray so number of table items is different each time.

Before and behind a table I have some vertical spaces (set by constraints) because I want to have some space between header/footer and table. So now a table is spread over whole "middle" part. This is OK.

What I want achieve to is that I don´t want to spread it over whole middle part. I want table to resize according to its content. If a table has only one row, I want to show one row and none of empty rows. I tried this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    

     int count = accController.connectedAccessories.count;

     [tableView setContentSize:CGSizeMake(tableView.contentSize.width, tableView.rowHeight * count)];
     return count;
}

But this doesn't work. If I add this line after setContentSize:

tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);

This works but If I have many of items, constraints are broken and table is over a footer. Any ideas how to do this? Thanks

EDIT: This header and footer aren´t part of the table. I just put label (I call it header) and button (footer) into main view. And between them I put a table. It looks better for my purpose. I call it header and footer but they are header/footer of the main view, not a table...

4 Answers 4

1

pass a blank view as viewForFooterInSection may help you.

Try this---

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    return view;
}
1

You have to try this code which solves your question

cell.selectionStyle=UITableViewCellSelectionStyleNone;

If you want to add backgroung image to the cells use this code

cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"image1.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
1

please do as per following in .h file

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *tempDataArray;
    UITableView *tblVIew;
}

@end

in .m file

#define cellHeight 30
#define headerHeight 50
#define footerHeight 50


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //[myMapView addAnnotation:(id<MKAnnotation>)];
    myScroll.hidden=YES;

    tempDataArray=[[NSMutableArray alloc] initWithObjects:@"Item1", nil];
    [self generateTableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tempDataArray count];
}

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

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
   }
    cell.textLabel.text=[tempDataArray objectAtIndex:indexPath.row];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return headerHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return footerHeight;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lblHeader=[[UILabel alloc] initWithFrame:CGRectMake(50, 5, 200, 30)];
    [lblHeader setText:@"Header"];
    [lblHeader setBackgroundColor:[UIColor clearColor]];
    [lblHeader setFont:[UIFont systemFontOfSize:20]];
    [lblHeader setTextColor:[UIColor redColor]];
    [lblHeader setTextAlignment:NSTextAlignmentCenter];
    return lblHeader;
}
// custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    CGRect frame = CGRectMake(10, 5, 220, 40);
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = frame;
            [button setTitle:(NSString *)@"Footer" forState:(UIControlState)UIControlStateNormal];
    [button setTintColor:[UIColor blackColor]];

            [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
            [button setBackgroundImage:[UIImage imageNamed:@"temp1.png"] forState:UIControlStateNormal];
        //[button setBackgroundColor:[UIColor whiteColor]];
            [myScroll addSubview:button];
    return button;
}
// custom view for footer. will be adjusted to default or specified footer height

-(void)buttonTapped:(id)sender
{
    // code for redirecting to another view
   // use button tag property for identifying perticular record
    [self generateTableView];
}

-(void)generateTableView
{
    tblVIew=nil;
    [tblVIew removeFromSuperview];

    [tempDataArray addObject:@" new item"];
    tblVIew=[[UITableView alloc] initWithFrame:CGRectMake(40, 50, 220,(cellHeight *[tempDataArray count])+headerHeight+footerHeight)];
    tblVIew.delegate=self;
    tblVIew.dataSource=self;
    [self.view addSubview:tblVIew];
    [tblVIew reloadData];
}

i hope this will help you.

0

Try belowing code. I hope it may help you

 -(void)viewDidLoad
{
   tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
   [tableView reloadData];
   tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, [self tableViewHeight] );

}
- (CGFloat)tableViewHeight
{
    [_tableView layoutIfNeeded];
    return [_tableView contentSize].height;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.