我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
使用这个子类,set separatorInset在iOS 9.2.1中不起作用,内容会被压缩。
@interface NSPZeroMarginCell : UITableViewCell
@property (nonatomic, assign) BOOL separatorHidden;
@end
@implementation NSPZeroMarginCell
- (void) layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
if (![view isKindOfClass:[UIControl class]]) {
if (CGRectGetHeight(view.frame) < 3) {
view.hidden = self.separatorHidden;
}
}
}
}
@end
https://gist.github.com/liruqi/9a5add4669e8d9cd3ee9
其他回答
所有答案的代码将使单元格填充为零,而不是默认值。我在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;
}
使用Swift 3并采用最快的黑客方法,您可以使用扩展来改进代码:
extension UITableViewCell {
var isSeparatorHidden: Bool {
get {
return self.separatorInset.right != 0
}
set {
if newValue {
self.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, 0)
} else {
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
}
}
}
然后,当你配置单元格时:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
switch indexPath.row {
case 3:
cell.isSeparatorHidden = true
default:
cell.isSeparatorHidden = false
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if cell.isSeparatorHidden {
// do stuff
}
}
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的单元格左侧看到分隔符。
我的开发环境是
Xcode 7.0 7A220 Swift 2.0 iOS 9.0
以上答案并不完全适用于我
经过尝试,我最终的工作解决方案是:
let indent_large_enought_to_hidden:CGFloat = 10000
cell.separatorInset = UIEdgeInsetsMake(0, indent_large_enought_to_hidden, 0, 0) // indent large engough for separator(including cell' content) to hidden separator
cell.indentationWidth = indent_large_enought_to_hidden * -1 // adjust the cell's content to show normally
cell.indentationLevel = 1 // must add this, otherwise default is 0, now actual indentation = indentationWidth * indentationLevel = 10000 * 1 = -10000
结果是:
对于那些专门寻找隐藏分隔线的尤里卡行,这是唯一的解决方案,为我工作:
row.cellUpdate { (cell, row) in
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: CGFloat.greatestFiniteMagnitude)
}