UItableView只有一个部分粘

问题描述 投票:1回答:2

我有一个UITableView,其中有很多部分。如果我将UITableView类型设为普通,则滚动时所有部分都会粘到顶部。

我的要求是只保留第0节的标题,其他标题应该浮动(就像你将样式设置为组时)。

另请注意,我的TableView具有崩溃和扩展功能,我使用这些部分。

我的UItableView样式设置为plain

public enum UITableViewStyle : Int {
    case plain // regular table view
}

我已经实现了自定义节头

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

任何正确方向的提示都非常受欢迎

ios swift uitableview
2个回答
0
投票

这种情况有一个棘手的解决方案。我试图在Swift中编写解决方案但是现在我无法找到解决方案,但我已经在Objective-C中实现了上述解决方案。

你需要子类化UITableView并公开私有api并覆盖它们,这样你就可以实现你想要的。

下面是一个TableView示例。

MYTableView.h

@interface MYTableView : UITableView
@property (assign,nonatomic)BOOL shouldFloatHeader;
@property (assign,nonatomic)BOOL shouldFloatFooter;
- (BOOL)allowsHeaderViewsToFloat;
- (BOOL)allowsFooterViewsToFloat;
@end

MYTableView.m

@implementation MYTableView
- (BOOL)allowsHeaderViewsToFloat{
    return _ shouldFloatHeader;
}
- (BOOL)allowsFooterViewsToFloat{
    return shouldFloatFooter;
}
@end

现在你也可以在swift上使用上面的TableView。我跑了并测试了它。

现在让第一部分变粘,所有其他部分不粘,我们需要在这里有一些技巧。下面是一个例子。有UITableViewDelegate方法didEndDisplayingHeader只是实现这个方法,并像这样切换shouldFloatHeader ..

Objective-C

-(void)tableView:(MYTableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (section >= 1) {
        tableView.shouldFloatHeader = YES;
    }else {
        tableView.shouldFloatHeader = NO;
    }
}

Swift

func tableView(_ tableView: MYTableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) {
        if section >= 1 {
            tableView.shouldFloatHeader = true
        }else {
            tableView.shouldFloatHeader = false
        }
    }

在某些情况下,使用私有API会导致您的应用程序拒绝。


-1
投票

正如我所说,你不能有一些粘性和一些浮动。相反,我建议使用不同的单元格作为除第0部分之外的每个部分的标题。仅对于第0部分,您应该使用节标题视图。使tableview样式仅为Plain。

© www.soinside.com 2019 - 2024. All rights reserved.