Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep UITableView padding on header but no in separator on IOS7

with the transition on iOS7 the separator has 15px padding on left. I know that i can remove this padding with separator inset feature on the UITableView setting in the xib file, but i need to keep heading text with padding. How to do it?

default:

enter image description here

with custom on separator inset to 0:

enter image description here

i need to keep separators like figure 2, but the header with "2013" like picture 1.

like image 230
Alessio Crestani Avatar asked Jan 09 '14 10:01

Alessio Crestani


3 Answers

USING SWIFT AND STORYBOARD

In storyboard you can set different separator insets for the TableView (header) and for Cells (separators).

Set the separator inset for cell to 0, by selecting the cell in Storyboard and setting the following:

enter image description here

Then set the separator inset for the TableView (for the header) to 15, by selecting the TableView in Storyboard and setting the following:

enter image description here

OPTIONAL:

You may also need to set the margins on the cells to be zero programmatically to ensure that the separator goes all the way from left to right. Use this code if needed in your table view controller's swift file:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("YOURCELL", forIndexPath: indexPath) as! UITableViewCell

    // THIS IS THE IMPORTANT PART...
    cell.layoutMargins = UIEdgeInsetsZero
    cell.preservesSuperviewLayoutMargins = false

    return cell

}
like image 187
Neiman Aleksei Avatar answered Nov 05 '22 16:11

Neiman Aleksei


for Seperator you can set it via Storyboard

and for header make a custom header like this

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
  UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
  UILabel *lblTitle = [UILabel.alloc initWithFrame:CGRectMake(6, 3, 136, 21)];

  [lblTitle setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]];
  [lblTitle setTextColor:[UIColor blackColor]];
  [lblTitle setTextAlignment:NSTextAlignmentLeft];
  [lblTitle setBackgroundColor:[UIColor clearColor]];
  [viewHeader addSubview:lblTitle];

  return viewHeader;
}

give it any particular height. and give it any text. make an Array for section Headers which will contain your years.

like image 15
Zeeshan Avatar answered Nov 05 '22 14:11

Zeeshan


UITableView has a delegate method named viewForHeaderInSection. If you remove the padding an then in the delegate method add a padding and return the section header view.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 200, 25)];

    // 15 pixel padding will come from CGRectMake(15, 5, 200, 25).
    label.text = @"2013";
    [view addSubview: label];
    return view;
}

You can design you header view how ever you like. For setting the height of the tableView header use:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

Hope it helps you... )

like image 2
Rashad Avatar answered Nov 05 '22 15:11

Rashad