【问题标题】:UITableView separator line does not show in iPhone appUITableView 分隔线不显示在 iPhone 应用程序中
【发布时间】:2013-11-12 04:50:14
【问题描述】:

我有一个使用 tableView 的应用程序,问题是当 tableView 有一条记录时,它不显示分隔线,但在有两条记录时显示。

tableView 中的第一个单元格是 textField。这是我正在使用的代码。

    for (UIView *subView in cell.subviews)
    {
    if (subView.tag == 2 || subView.tag == 22) 
    {
        [subView removeFromSuperview];
    }
    }

   tableView.backgroundView=nil;

   tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

   tableView.separatorInset = UIEdgeInsetsZero;


   if(indexPath.section==0){

   tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,0,248,31)];



    tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    tagInputField.textAlignment=UITextAlignmentLeft;
    tagInputField.backgroundColor=[UIColor whiteColor];

    tagInputField.tag = 2;
    tagInputField.delegate = self;
    tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;


    [tagInputField.layer setCornerRadius:5.0f];
    [tagInputField.layer setMasksToBounds:YES];
    tagInputField.layer.borderWidth = 0.3;
    tagInputField.layer.borderColor = [UIColor darkGrayColor].CGColor;


    tagInputField.font=[UIFont fontWithName:@"Myriad-Pro" size:8];
    [tagInputField setText:@"Enter tag here "];
    tagInputField.textColor =[UIColor grayColor];





    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    [cell addSubview:tagInputField];






    return cell;



}


if(indexPath.section==1) {
    UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 8, 18, 18)];
    crossButton.tag = 22; //use a tag value that is not used for any other subview
    //crossButton.backgroundColor = [UIColor purpleColor];
    crossButton.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cross.png"]];
    [cell addSubview:crossButton];
    cell.textLabel.font =[UIFont fontWithName:@"Myriad-Pro" size:8];
    cell.textLabel.textColor =[UIColor grayColor];

    cell.backgroundColor=[UIColor whiteColor];

    cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
    [crossButton addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];


    [tagInputField setFrame:CGRectMake(5,0,248,31)];


    tableView.backgroundColor=[UIColor whiteColor];


    [tagInputField.layer setCornerRadius:0.0f];
    [tagInputField.layer setMasksToBounds:YES];


    tagInputField.layer.borderWidth = 0.0;
    tagInputField.layer.borderColor = [UIColor clearColor].CGColor;




    return cell;
}

【问题讨论】:

  • 尝试在单元格中添加文本字段,如 [cell.contentView addSubview:tagInputField];
  • 您的单元格高度可能更高,因此它会覆盖在 saperator 线上
  • 尝试tableView.separatorColor = [UIColor redColor];

标签: ios uitableview


【解决方案1】:

当您只有 1 条记录时,分隔符不会显示,您应该在 viewDidLoad 处为 tableView 设置分隔符,如下所示

- (void)viewDidLoad
{
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

在任何情况下,如果你想为每个单元格显示你自己的分隔符,十尝试添加一个 imageView 或表格单元格共享的东西

UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
separatorView.layer.borderColor = [UIColor redColor].CGColor;
separatorView.layer.borderWidth = 1.0;
[cell.contentView addSubview:separatorView];

How to customize tableView separator in iPhone

了解更多……

【讨论】:

  • 你没有给出任何其他内容的同一行
【解决方案2】:

只需在 Cell_for_Row_At_Index_path 委托方法中尝试这样:

[tableView setSeparatorInset:UIEdgeInsetsZero];
    [tableView setSeparatorColor:[UIColor greenColor]];

只需粘贴此行并检查。

【讨论】:

    【解决方案3】:

    在表格视图的末尾添加一个空页脚以显示分隔符。为此,请在您的类中使用其他 tableview 委托函数实现以下 tableview 委托方法:

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.001; }

    【讨论】:

      【解决方案4】:

      我用它来添加最后一个单元格分隔线....

        - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
              {
                  return 1.0f;
              }
      
              - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
              {
                  // To "clear" the footer view
                  UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
                  separatorLine.layer.borderColor = [UIColor grayColor].CGColor;
                  separatorLine.layer.borderWidth = 1.0;
                  return separatorLine;
              }
      

      【讨论】:

        【解决方案5】:

        我发现上述答案均无效。我特别警惕建议添加 UIView 看起来像线条的答案。

        最后我发现这个答案对我有用:

        tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
        

        在另一个答案中,有人建议添加效果更好

        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
        

        但我发现这是不必要的。

        所有功劳归于user859045samvermette,感谢他们在不同线程上的回答。 samvermette 的帖子特别有很多关于这个主题的答案,非常有帮助,值得一看。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-15
          • 2021-10-21
          • 2011-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多