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

请稍等,因为这是一个长时间的解释

我有一个UIViewController它由一个UIButton和一个UITableView加载不同types的UITableViewCell与标识符Cell2Cell2 ,在事件touchUpInside的button。 我使用故事板。

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

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

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

每次单击tableViewCell之外的button时,都会根据单元标识符交换tableViewCell

最初, tableView占据了viewController的完整宽度,由viewController组成,但button被轻敲, tableViewCell被更改为Cell2, tableViewCell的框被更改,宽度减less10,x-origin增加5 。

但是,当发生这种情况时, Cell2的分隔符与Cell2的单元格相距5个像素,而左侧的单元距离5个像素。 所有加载数据的Cell2发生这种情况,并且没有dataframe的单元会被适当地改变。

但之后的单元格宽度为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; } 

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

在下面的代码中更改UIView hight / width / color / image来设置你的separatorLine。

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

 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的子类(如果没有在其中添加分隔符图像variables)以及每个单元格创build,只需更改图像和框架,而不是在每个单元格上添加分隔符重绘。 如果你需要一个代码,我也可以提供。 目前,当单元格重新绘制时,它已经添加了上次添加的图像,并且只是再次添加图像,无论是在-prepareForReuse方法中删除,还是按照上面的解释。

 ***** Custom Cell ***** // // CustomCell.m // Custom // // Created by Syed Arsalan Pervez on 2/8/13. // Copyright (c) 2013 SAPLogix. All rights reserved. // #import "CustomCell.h" @implementation CustomCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code _separatorImage = [[UIImageView alloc] initWithFrame:CGRectZero]; [[self contentView] addSubview:_separatorImage]; } return self; } - (void)prepareForReuse { _separatorImage.image = nil; } - (void)dealloc { [_separatorImage release]; [super dealloc]; } @end 

在视图控制器中使用上述单元格。

 ***** Test View Controller ***** // // TestViewController.m // Custom // // Created by Syed Arsalan Pervez on 2/8/13. // Copyright (c) 2013 SAPLogix. All rights reserved. // #import "TestViewController.h" #import "CustomCell.h" @interface TestViewController () @end @implementation TestViewController - (void)viewDidLoad { [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; #warning TODO: set the image name here _separatorImage1 = [[UIImage imageNamed:@""] retain]; _separatorImage2 = [[UIImage imageNamed:@""] retain]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *_identifier = @"CustomCell"; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:_identifier]; if (!cell) { cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_identifier] autorelease]; } //Set Separator Image Here //Preload the image so it doesn't effect the scrolling performance CGRect frame = cell.contentView.frame; switch (indexPath.row) { case 0: cell.separatorImage.image = _separatorImage1; cell.separatorImage.frame = CGRectMake(0, CGRectGetMaxY(frame)-1, frame.size.width, 1); break; case 1: cell.separatorImage.image = _separatorImage2; cell.separatorImage.frame = CGRectMake(frame.origin.x+5, CGRectGetMaxY(frame)-1, frame.size.width-10, 1); break; } return cell; } - (void)dealloc { [_separatorImage1 release]; [_separatorImage2 release]; [super dealloc]; } @end 

我这样做…希望这可能有帮助。

 // // UITableViewCell+MyAdditions.h // // Created by Roberto O. Buratti on 19/02/14. // #import <UIKit/UIKit.h> @interface UITableViewCell (MyAdditions) @property (nonatomic,assign) UITableViewCellSeparatorStyle cellSeparatorStyle; @property (nonatomic,strong) UIColor *cellSeparatorColor; @end // // UITableViewCell+MyAdditions.m // // Created by Roberto O. Buratti on 19/02/14. // #import "UITableViewCell+MyAdditions.h" NSString *const kUITablewViewCellSeparatorLayerName = @"kUITablewViewCellSeparatorLayerName"; @implementation UITableViewCell (MyAdditions) -(CALayer *)separatorLayer { for (CALayer *sublayer in self.layer.sublayers) { if ([sublayer.name isEqualToString:kUITablewViewCellSeparatorLayerName]) return sublayer; } return nil; } -(CALayer *)newSeparatorLayer { CALayer *separatorLayer = [CALayer layer]; separatorLayer.name = kUITablewViewCellSeparatorLayerName; separatorLayer.frame = CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1); separatorLayer.backgroundColor = [UIColor whiteColor].CGColor; [self.layer addSublayer:separatorLayer]; return separatorLayer; } -(UITableViewCellSeparatorStyle)cellSeparatorStyle { CALayer *separatorLayer = [self separatorLayer]; if (separatorLayer == nil) return UITableViewCellSeparatorStyleNone; else return UITableViewCellSeparatorStyleSingleLine; } -(void)setCellSeparatorStyle:(UITableViewCellSeparatorStyle)separatorStyle { CALayer *separatorLayer = [self separatorLayer]; switch (separatorStyle) { case UITableViewCellSeparatorStyleNone: [separatorLayer removeFromSuperlayer]; break; case UITableViewCellSeparatorStyleSingleLine: if (separatorLayer == nil) separatorLayer = [self newSeparatorLayer]; break; default: @throw [NSException exceptionWithName:NSStringFromClass([self class]) reason:@"Unsupported separatorStyle" userInfo:nil]; break; } } -(UIColor *)cellSeparatorColor { CALayer *separatorLayer = [self separatorLayer]; return [UIColor colorWithCGColor:separatorLayer.backgroundColor]; } -(void)setCellSeparatorColor:(UIColor *)separatorColor { CALayer *separatorLayer = [self separatorLayer]; if (separatorLayer == nil) separatorLayer = [self newSeparatorLayer]; separatorLayer.backgroundColor = separatorColor.CGColor; } @end 

现在你可以做类似的事情了

 UITableViewCell *cell = ... cell.cellSeparatorStyle = UITableViewCellSeparatorStyleSingleLine; cell.cellSeparatorColor = [UIColor orangeColor]; 

正如别人所说,通常有两种方法可以做到这一点,要么创build一个CALayer或1px的UIView分隔符,并将其添加到contentView

随着时间的推移,我已经看到多个项目以不同的方式做到这一点,甚至有时甚至在同一个项目中有多种不同的方式。 由于单元重用,也容易引入错误,而且为了正确渲染像素线,必须合并屏幕比例: (1.0 / [UIScreen mainScreen].scale)

我创build了一个库 ,将其简化为一个方法类,不需要子类。 https://github.com/kgaidis/KGViewSeparators

Objective-C

[view kg_show:YES separator:KGViewSeparatorTop color:[UIColor blackColor] lineWidth:KGViewSeparatorLineWidth(1.0) insets:UIEdgeInsetsMake(0, 15.0, 0, 15.0)];

迅速:

view.kg_show(true, separator: .Bottom, color: UIColor.blackColor(), lineWidth: KGViewSeparatorLineWidth(1.0), insets: UIEdgeInsetsZero)