我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
在tableview单元格类中。放这行代码
separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: self.bounds.size.width)
其他回答
iOS 7和8的更好解决方案
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(@"");
if (cell && indexPath.row == 0 && indexPath.section == 0) {
DLog(@"cell.bounds.size.width %f", cell.bounds.size.width);
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.0f);
}
}
如果你的应用程序是可旋转的-使用3000.0f作为左插入常数或在运行中计算它。 如果你尝试设置右插入,你会在iOS 8的单元格左侧看到分隔符。
在使用iOS 8.4的Swift中:
/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
if indexPath.row == 3 {
// Hiding separator line for only one specific UITableViewCell
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
}
}
Note: this snippet above will work on UITableView using dynamic cells. The only problem that you can encounter is when you use static cells with categories, a separator type different than none and a grouped style for the table view. In fact, in this particular case it will not hide the last cell of each category. For overcoming that, the solution that I found was to set the cell separator (through IB) to none and then creating and adding manually (through code) your line view to each cell. For an example, please check the snippet below:
/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
// Row 2 at Section 2
if indexPath.row == 1 && indexPath.section == 1 {
// Hiding separator line for one specific UITableViewCell
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
// Here we add a line at the bottom of the cell (e.g. here at the second row of the second section).
let additionalSeparatorThickness = CGFloat(1)
let additionalSeparator = UIView(frame: CGRectMake(0,
cell.frame.size.height - additionalSeparatorThickness,
cell.frame.size.width,
additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.redColor()
cell.addSubview(additionalSeparator)
}
}
来跟进Hiren的回答。
在ViewDidLoad和下面一行:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
或者,如果你正在使用XIB或storyboard,将“separator”更改为“none”:
在CellForRowAtIndexPath中添加这个:
CGFloat separatorInset; // Separator x position
CGFloat separatorHeight;
CGFloat separatorWidth;
CGFloat separatorY;
UIImageView *separator;
UIColor *separatorBGColor;
separatorY = cell.frame.size.height;
separatorHeight = (1.0 / [UIScreen mainScreen].scale); // This assures you to have a 1px line height whatever the screen resolution
separatorWidth = cell.frame.size.width;
separatorInset = 15.0f;
separatorBGColor = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];
separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
separator.backgroundColor = separatorBGColor;
[cell addSubView: separator];
这是一个结果的例子,我显示一个动态单元格的tableview(但只有一个内容)。结果是,只有一个有分隔符,而不是所有的“虚拟”tableview自动添加填充屏幕。
编辑:对于那些不经常阅读注释的人来说,实际上有一种更好的方法,只需几行代码:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
对于那些专门寻找隐藏分隔线的尤里卡行,这是唯一的解决方案,为我工作:
row.cellUpdate { (cell, row) in
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: CGFloat.greatestFiniteMagnitude)
}
所有答案的代码将使单元格填充为零,而不是默认值。我在iOS 11 iPad Pro 12中看到了这个问题。”
但我有一个解决方案(“肮脏的hack”),那就是使一个空的部分,将使分隔线隐藏。
下面是我使用的代码:
typedef enum PXSettingsTableSections {
PXSettingSectionInvite = 0,
PXSettingSectionAccount,
PXSettingSectionPrivacy,
PXSettingSectionCreation,
PXSettingSectionTest,
PXSettingSectionAboutHide, // invisble section just to hide separator Line
PXSettingSectionFooterHide, // invisble section just to hide separator Line
PXSettingSectionNumItems,
} PXSettingsTableSectionsType;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return PXSettingSectionNumItems;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case PXSettingSectionInvite: return 1;
case PXSettingSectionAccount:return (isSocialLogin) ? 1 : 2;
case PXSettingSectionPrivacy: return 1;
case PXSettingSectionCreation: return 2;
case PXSettingSectionTest: return 3;
case PXSettingSectionAboutHide: return 3;
default: return 0;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch(section)
{
case PXSettingSectionInvite: return nil;
case PXSettingSectionAccount: return @"Account";
case PXSettingSectionPrivacy: return @"Privacy";
case PXSettingSectionCreation: return @"Someting";
case PXSettingSectionTest: return @"Test";
case PXSettingSectionAboutHide: return @" ";
case PXSettingSectionFooterHide: return @" ";
}
return nil;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
if (section == PXSettingSectionFooterHide || section == PXSettingSectionAboutHide) {
// [UIColor clearColor] will not work
[header.contentView setBackgroundColor:[UIColor whiteColor]];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
// You can control here the size of one specific section
if(section == PXSettingSectionAboutHide){
return 0.0000001; //make it real small
}
return 45.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch(indexPath.section)
{
case PXSettingSectionInvite:
return self.inviteCell;
case PXSettingSectionAccount:
if (isSocialLogin) {
return self.myWalletCell;
}
switch(indexPath.row)
{
case 0: return self.changePasswordCell;
case 1: return self.myWalletCell;
}
case PXSettingSectionPrivacy:
switch(indexPath.row)
{
case 0: return self.privateAccountCell;
}
case PXSettingSectionCreation:
switch(indexPath.row)
{
case 0: return self.videoResolutionCell;
case 1: return self.selfieMirrorCell;
}
case PXSettingSectionTest:
switch(indexPath.row)
{
case 0: return self.termsOfUseImageCell;
case 1: return self.attributionCell;
case 2: return self.aboutCell;
}
case PXSettingSectionAboutHide:{
switch(indexPath.row)
{
case 0: return self.clearCacheCell;
case 1: return self.feedbackCell;
case 2: return self.logoutCell;
}
}
}
return self.emptyCell;
}