Keep UITableView padding on header but no in separator on IOS7

别说谁变了你拦得住时间么 提交于 2019-12-03 09:37:12
Xeieshan

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.

Neiman Aleksei

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:

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

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

}
Rashad

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... )

You can set insets on table (and header/footer) and on the cell itself:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
    }    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0, 10, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

If you want to indent the Section text from the left margin, here is a simple way I did it. In the function, see the space after the first quote mark? Just put as many spaces as you need before the start of the text.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let title: UILabel = UILabel()

        title.text = " \(sortedSections[section])"
        title.textAlignment = NSTextAlignment.Left

        return title
    }

You might not want to create a custom header view always because sometimes its a overkill or it'll need some trial and error to make it exactly like the default section title. Instead there is a simple hack which won't need to return a custom header view. Just set your separator indent to 0 and put some leading space in the title string.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"    Your Title";
}

in you cellForRowAtIndeaPath method add a view into cell like

UIView* view = [[UIView alloc] initWithFrame:CGrectmake(cell.frame.ori.x, cell.frame.ori.y, 20.0, cell.height)];

view.backgroundColor = desired color

[cell.containView addSubView: view];

add this into your first cell code and you are done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!