如何向UITableViewCell添加自定义分隔符?

use*_*840 12 objective-c ios

请稍等一下,因为这是一个很长的解释

我有一个UIViewController由a UIButton和a 组成的,UITableViewUITableViewCell用标识符加载不同类型的s,Cell1并且在按钮Cell2事件touchUpInside时加载.我正在使用故事板.

两个单元的分隔符都是自定义的.

Cell1 有一个分隔符占据单元格的整个宽度和单元格底部的1个像素高度.

Cell2有一个分隔符,它与左边和右边的单元格偏移了5个像素.

每个外侧的按钮时tableView被点击的tableViewCells的交换,基于所述小区标识符.

最初tableView占据Cell1 的完整宽度viewController并由tableViewCellCell1 组成,但是按钮被轻敲,s被更改为Cell2并且框架tableView被更改,宽度减少10并且x原点增加5.

但是当发生这种情况时,分隔符与Cell2右边的单元格相距5个像素,但是在左边则偏离5个像素.对于Cell2加载了数据的所有数据都会发生这种情况,而没有数据框架的单元格会被适当地更改.

但之后的单元格宽度Cell1(宽度更大)

-(void)setSeperatorStyleForTableView :(UITableViewCell *)cell //this is called in cellForRowAtIndex 
{
   //cell- type of cell(Cell1 or Cell2)

     CGRect seperatorFrame;
    UIImageView *seperatorImage;

    seperatorFrame = [self setSeperatorFrame:cell];

    if(firstCellToBeLoaded)//BOOL used to change the button text and load appropriate cells
    {
        seperatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table_row         
                                                                            2.png"]];
    }
    else
    {

        seperatorImage = [[UIImageView alloc] initWithImage:[UIImage   
                                                  imageNamed:@"table_row.png"]];
    }
    seperatorImage.frame = seperatorFrame;
    seperatorImage.autoresizingMask = YES;
    [cell.contentView addSubview:seperatorImage];

}

//set the customized separator frame

-(CGRect)setSeperatorFrame :(UITableViewCell *)cell
{

    CGRect seperatorFrame;
    seperatorFrame.size.height = 1.0;
    seperatorFrame.origin.y = cell.frame.origin.y + (cell.frame.size.height - 1.0);

    if(firstCellToBeLoaded)
    {
        seperatorFrame.origin.x = cell.frame.origin.x ;
        seperatorFrame.size.width = cell.frame.size.width;
    }
    else
    {
        seperatorFrame.origin.x = cell.frame.origin.x + 5.0;
        seperatorFrame.size.width = cell.frame.size.width -10.0;

    }

    return seperatorFrame;
}
Run Code Online (Sandbox Code Playgroud)

iPa*_*tel 44

您可以添加tableView标准分隔线,并在每个单元格的顶部添加自定义行.

在下面的代码更改HIGHT /宽/彩色/图像UIView用于设置您的separatorLine.

添加自定义分隔符的最简单方法是添加UIView1px高度的简单:

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
Run Code Online (Sandbox Code Playgroud)

这段代码可能会解决你的问题:)