首页 文章

在 Headers 上保留UITableView填充但在IOS7上的分隔符中没有

提问于
浏览
23

在iOS7上进行转换时,分隔符左侧有15px填充 . 我知道我可以在xib文件中的UITableView设置中使用分隔符插入功能删除此填充,但我需要使用填充保持 Headers 文本 . 怎么做?

默认:

enter image description here

使用自定义分隔符插入0:

enter image description here

我需要像图2那样保留分隔符,但 Headers 为“2013”,如图1所示 .

7 回答

  • 13

    在你的cellForRowAtIndeaPath方法中添加一个视图到单元格中

    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];
    

    将其添加到您的第一个单元格代码中,您就完成了

  • 2

    对于Seperator,您可以通过Storyboard进行设置

    并为 Headers 创建这样的自定义 Headers

    - (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;
    }
    

    给它任何特定的高度 . 并给它任何文字 . 为包含年份的部分 Headers 创建一个数组 .

  • -4

    USING SWIFT AND STORYBOARD

    在故事板中,您可以为TableView( Headers )和单元格(分隔符)设置不同的分隔符插入 .

    通过在Storyboard中选择单元格并设置以下内容,将单元格的分隔符插入设置为0:

    enter image description here

    然后通过在Storyboard中选择TableView并设置以下内容,将TableView(对于 Headers )的分隔符插入设置为15:

    enter image description here

    OPTIONAL:

    您可能还需要以编程方式将单元格上的边距设置为零,以确保分隔符从左向右一直运行 . 如果需要在表视图控制器的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
    
    }
    
  • 1

    UITableView 有一个名为 viewForHeaderInSection 的委托方法 . 如果删除填充,则在委托方法中添加填充并返回节 Headers 视图 .

    - (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;
    }
    

    您可以根据自己的喜好设计 Headers 视图 . 要设置tableView标头的高度,请使用:

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

    希望它可以帮助你...)

  • 39

    您可以在表(和页眉/页脚)以及单元格本身上设置insets:

    -(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];
        }
    }
    
  • 1

    如果你想从左边距缩进Section文本,这是一个简单的方法 . 在函数中,请看第一个引号后面的空格?只需在文本开头之前放置尽可能多的空格 .

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let title: UILabel = UILabel()
    
            title.text = " \(sortedSections[section])"
            title.textAlignment = NSTextAlignment.Left
    
            return title
        }
    
  • 0

    您可能不希望始终创建自定义 Headers 视图,因为有时候它是一个过度杀手,或者它需要一些试验和错误才能使它与默认的 Headers Headers 完全相同 . 相反,有一个简单的hack,不需要返回自定义 Headers 视图 . 只需将分隔符缩进设置为0,并在 Headers 字符串中放置一些前导空格 .

    enter image description here

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

相关问题