如何向UITableViewCell添加自定义分隔符? [英] How to add a custom separator to UITableViewCell?

查看:100
本文介绍了如何向UITableViewCell添加自定义分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please spare sometime as this is a long explanation

我有一个 UIViewController ,其中包含 UIButton 和一个 UITableView ,它使用Identifier加载不同类型的 UITableViewCell Cell1 Cell2 ,按钮的事件 touchUpInside 。我正在使用故事板。

I have a UIViewController which consists of a UIButton and a UITableView which loads different types of UITableViewCells with Identifier Cell1 and Cell2, on event touchUpInside of the button. I m using storyboard.

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

The separator for both cells are customized.

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

Cell1 has a separator that occupies the entire width of cell and 1 pixel height at the bottom of the cell.

Cell2 有一个分隔符,它与单元格的左右偏移量为5像素。

Whereas Cell2 has a separator which has offset of 5 pixels from the cell, both left and right.

每次按下<$ c之外的按钮单击$ c> tableView 根据单元格标识符交换 tableViewCell

Each time the button outside the tableView is clicked the tableViewCells are swapped, based on the cell identifier.

最初 tableView 占用 viewController 的完整宽度,由Cell1组成,但按下该按钮, tableViewCell s更改为Cell2并且 tableView 的框架已更改,宽度减少10并且x-原点增加5。

Initially the tableView occupies the complete width of viewController and consists of Cell1, but the the button is tapped , tableViewCells are changed to Cell2 and the frame of the tableView is Changed, The width is reduced by 10 and x-origin is increased by 5.

但是当发生这种情况时, Cell2 的分隔符离单元格5个像素在右边,但在左边,它是5像素。
对于加载了数据的所有 Cell2 都会发生这种情况,并且没有相应数据的单元格会被更改。

But when this happens, the separator of Cell2 is 5 pixels away from the cell on right but on the left it is away by 5 pixel. This happens for all Cell2 which is loaded with data, and the cells which has no data the frame is changed appropriately.

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

But the cell after that has the width of Cell1 (larger width)

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


推荐答案

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

You can add tableView's standard separator line, and add your custom line at the top of each cell.

在以下代码中更改 hight / width / color / image UIView 用于设置separatorLine。

In following code Change hight/width/color/image of UIView for set your separatorLine.

添加自定义分隔符的最简单方法是添加1px高度的简单 UIView

The easiest way to add custom separator is to add simple UIView of 1px height:

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

此代码可能会解决您的问题:)

这篇关于如何向UITableViewCell添加自定义分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆