【问题标题】:How to set an image for Cell Separator in iOS [duplicate]如何在iOS中为单元格分隔符设置图像[重复]
【发布时间】:2013-12-12 05:43:10
【问题描述】:

在我的表格视图中,我需要将单元格分隔符设置为我随身携带的图像。我该怎么做?

【问题讨论】:

标签: ios uitableview


【解决方案1】:

一个简单的解决方案是您可以将图案图像设置为单元格分隔符。

[tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:...]]];

或者你可以简单地在单元格内容视图中添加一个 UIImageView

UIImageView *imagView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seprater.png"]];
imgView.frame = CGRectMake(0,cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1);
[customCell.contentView addSubview:imageView];

【讨论】:

  • 第一个代码解决了我的问题。但我的单元格分隔符似乎与右侧对齐。有什么办法可以解决吗?
  • @HarikrishnanT 这是 ios 7 的问题。你应该将它用于 iOS7 - if ([tblvw respondsToSelector:@selector(setSeparatorInset:)]) [tblvw setSeparatorInset:UIEdgeInsetsZero];
【解决方案2】:

您最好的选择可能是将表格的 separatorStyle 设置为 UITableViewCellSeparatorStyleNone。

并在需要时手动添加/绘制一条线(可能在 tableView:cellForRowAtIndexPath:) 中。

tableView:cellForRowAtIndexPath:

...

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Drawing our own separatorLine here because I need to turn it off for the
    // last row. I can only do that on the tableView and on on specific cells.
    // The y position below has to be 1 less than the cell height to keep it from
    // disappearing when the tableView is scrolled.
    UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

    [separatorLine release];
}

// 设置默认单元格设置。

...
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
...
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;

... 在 viewDidLoad 中:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

【讨论】:

    【解决方案3】:

    要激活它,请在单元格底部放置一个图像视图并为其分配一个图像。还请确保您已将 cellSeperatorStyle 设置为无

    应该可以的

    【讨论】:

      【解决方案4】:
      {    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];}
      
          -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
          {
               UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"<image name>"]];
              imgView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
              [customCell.contentView addSubview:imgView];
               return  customCell;
      
           }
      }
      

      【讨论】:

        【解决方案5】:

        首先,您必须通过以下代码将 SeparatorStyle 设为无

        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        

        然后在每个单元格的末尾添加图像

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
        {
             ..... 
        
             UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seprater.png"]];
             [imgView sizeToFit];
             [Cell.contentView addSubview:imgView];
        
             .....
        
         }
        

        【讨论】:

          猜你喜欢
          • 2018-09-04
          • 1970-01-01
          • 1970-01-01
          • 2019-05-02
          • 2017-08-21
          • 2014-01-01
          • 2023-03-04
          • 1970-01-01
          • 2018-03-11
          相关资源
          最近更新 更多