随着iOS7的更新,表格分隔符左边有15像素的内边距。我知道可以通过UITableView设置中的分隔符插入特性来去除这个内边距,但我需要保留具有内边距的标题文本。如何做到这一点?
默认情况下如第一张图片所示:
通过将分隔符插入自定义为0,可以实现如第二张图片所示的效果:
我需要保留类似第一张图片中“2013”标题的分隔符样式。
默认情况下如第一张图片所示:
![enter image description here](https://istack.dev59.com/Zmuay.webp)
![enter image description here](https://istack.dev59.com/ou9bX.webp)
使用 Swift 和 Storyboard
在Storyboard中,您可以为TableView(标题)和单元格(分隔符)设置不同的分隔符插入。
通过在Storyboard中选择单元格并设置以下内容,将单元格的分隔符插入设置为0:
然后通过在Storyboard中选择TableView并设置以下内容,将TableView(标题)的分隔符插入设置为15:
可选:
如果需要确保分隔符从左到右覆盖所有宽度,您还可能需要以编程方式将单元格的边距设置为零。 在您的table view controller's Swift文件中,如有需要,请使用以下代码:
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
}
.separatorInset
属性。将单元格的属性设置为.zero
,让表格的属性成为您想要的值。 - NSRover您可以通过Storyboard来设置分隔符。
对于标题,您可以像这样制作自定义标题。
- (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;
}
给它任何特定的高度,并给它任何文本。为部分标题创建一个数组,其中包含您的年份。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
是一个代理函数。将其放置在您的UITableView视图控制器中。 - Zeeshan-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
方法中,使用[lblTitle setText:[array objectAtIndex:section]
来设置标题。 - ZeeshanUITableView
有一个名为viewForHeaderInSection
的委托方法。如果您删除填充,然后在委托方法中添加填充并返回部分标题视图。
- (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;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
-(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];
}
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let title: UILabel = UILabel()
title.text = " \(sortedSections[section])"
title.textAlignment = NSTextAlignment.Left
return title
}
有时候你可能不想总是创建自定义的表头视图,因为这有时会过度设计或需要一些试错才能使其与默认的分区标题完全相同。相反,有一个简单的技巧可以避免使用自定义表头视图。只需将分隔符缩进设置为0,并在标题字符串中添加一些前导空格即可。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @" Your Title";
}
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];
将此代码添加到您的第一个单元格中即可完成