即时隐藏Tableview部分标题

时间:2015-04-04 16:20:42

标签: ios objective-c uitableview

当填充tableview时,我正在尝试隐藏和显示节标题。我现在的代码偶尔会按预期工作,但大多数情况下,部分标题中的一个或两个将保持显示状态(但是,如果我在没有数据的情况下将标题拖离屏幕,它将按预期消失)。

这是我用来隐藏/取消隐藏部分的代码。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    //Create custom header titles for desired header spacing
    if (section == 0) {
        UIView *headerView;
        if (self.searchUsers.count || self.searchTracks.count)
        {
            headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        }
        else{
            headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        }
        HeaderTitleLabel *headerTitle = [[HeaderTitleLabel alloc] initWithFrame:CGRectMake(17, 10, 150, 20) headerText:@"USERS"];
        [headerView addSubview:headerTitle];
        return headerView;
    }
    else if (section == 1){
        UIView *headerView;
        if (self.searchUsers.count || self.searchTracks.count)
        {
            headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        }
        else{
            headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        }
        HeaderTitleLabel *headerTitle = [[HeaderTitleLabel alloc] initWithFrame:CGRectMake(17, 0, 150, 20) headerText:@"SONGS"];
        [headerView addSubview:headerTitle];
        return headerView;
    }
    else
        return  nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //custom header spacing
    if (section == 0) {
        if (self.searchUsers.count || self.searchTracks.count)
        {
            return 40;
        }
        else
            return 0;
    }
    else if (section == 1) {
        if (self.searchUsers.count || self.searchTracks.count)
        {
            return 50;
        }
        else
            return 0;
    }
    else
        return 0;
}

我检查我的数组是否有对象,如果没有,则将帧的高度设置为0.这似乎不起作用。有什么想法我应该怎么做呢?谢谢。

4 个答案:

答案 0 :(得分:0)

您正在使用错误的表格视图方法。你能澄清一下你到底想做什么吗?我可以帮你。而不是UIView使用UITableView Cell。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.searchUsers.count || self.searchTracks.count)
        {
 if (indexPath.section ==0) {
        static NSString *simpleTableIdentifier = @"CategoryCell";



    CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }


    cell.lbl_CatName.text = [[self.CategoryArray objectAtIndex:indexPath.row] objectForKey:kNAME];

    return cell;}
else if (indexPath.section ==1){


 static NSString *EarnPointIdentifier = @"EarnPointTableCell";

    EarnPointTableCell *EarnPointcell = (EarnPointTableCell *)[tableView dequeueReusableCellWithIdentifier:EarnPointIdentifier];
    if (EarnPointcell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EarnPointTableCell" owner:self options:nil];
        EarnPointcell = [nib objectAtIndex:0];
    }
    return EarnPointcell ;
  }  

else{
//it will have transparent background and every thing will be transparent
    static NSString *RewardIdentifier = @"RewardTableCell";

    RewardTableCell *RewardCell = (RewardTableCell *)[tableView dequeueReusableCellWithIdentifier:RewardIdentifier];
    if (RewardCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RewardTableCell" owner:self options:nil];
        RewardCell = [nib objectAtIndex:0];
    }

    return RewardCell ;


  }


}


}

答案 1 :(得分:0)

您的模型应该驱动您的表视图。不要只返回“'行数'您的模型中的价值,以及一些'部分'值。

考虑一个包含两个数组作为模型的数组。

答案 2 :(得分:0)

现在似乎工作正常。我做的是创建布尔值,以便在我的数据源为零时跟踪我的数据。像这样;

 [SoundCloudAPI getTracksWithSearch:userInput userID:nil withCompletion:^(NSMutableArray *tracks) {
        self.searchTracks = tracks;
        [self.tableView beginUpdates];
        if (tracks.count) {
            self.songsHeaderIsHidden = NO;
        }
        else
            self.songsHeaderIsHidden = YES;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
        [[self.tableView headerViewForSection:1] setNeedsLayout];
        [[self.tableView headerViewForSection:1] setNeedsDisplay];
        [self.tableView endUpdates];
    }];

然后相应地设置标题..

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    //Create custom header titles for desired header spacing
    if (section == 0) {
        if (self.userHeaderIsHidden) {
            return nil;
        }
        else {
        UIView *userHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        HeaderTitleLabel *usersHeaderTitle = [[HeaderTitleLabel alloc] initWithFrame:CGRectMake(17, 10, 150, 20) headerText:@"USERS"];
        [userHeaderView addSubview:usersHeaderTitle];
        return userHeaderView;
        }
    }
    else if (section == 1){
        if (self.songsHeaderIsHidden) {
            return nil;
        }
        else {
        UIView *songsHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        songsHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        HeaderTitleLabel *songsHeaderTitle = [[HeaderTitleLabel alloc] initWithFrame:CGRectMake(17, 0, 150, 20) headerText:@"SONGS"];
        [songsHeaderView addSubview:songsHeaderTitle];
        return songsHeaderView;
        }
    }
    else
        return  nil;
}

答案 3 :(得分:0)

就个人而言,我会使用UICollectionView并编写您自己的布局。这样,您可以随时明确地布局节标题的位置。最好的方法可能是UICollectionViewFlowLayout的子类,因为您可以免费获得所有超类属性(例如itemSizeheaderReferenceSize等。)

这是我为具有粘贴标题而编写的实现,例如Instagram应用程序。

请注意,在prepareLayout中,我只计算每个元素的正常位置。然后在layoutAttributesForElementInRect中,我通过并找出放置标题的位置。您可以在此处查看是否应显示标题。

import UIKit

class BoardLayout: UICollectionViewFlowLayout {
    private var sectionFrames: [CGRect] = []
    private var headerFrames: [CGRect] = []
    private var footerFrames: [CGRect] = []
    private var layoutAttributes: [UICollectionViewLayoutAttributes] = []
    private var contentSize: CGSize = CGSizeZero

    override func prepareLayout() {
        super.prepareLayout()
        self.sectionFrames.removeAll(keepCapacity: false)
        self.headerFrames.removeAll(keepCapacity: false)
        self.footerFrames.removeAll(keepCapacity: false)
        self.layoutAttributes.removeAll(keepCapacity: false)
        let sections = self.collectionView?.numberOfSections()
        var yOffset: CGFloat = 0.0
        for var i: Int = 0; i < sections; i++ {
            let indexPath = NSIndexPath(forItem: 0, inSection: i)
            var itemSize: CGSize = self.itemSize
            if let d = self.collectionView?.delegate as? UICollectionViewDelegateFlowLayout {
                if let collection = self.collectionView {
                    if let size = d.collectionView?(collection, layout: self, sizeForItemAtIndexPath: NSIndexPath(forItem: 0, inSection: i)) {
                        itemSize = size
                    }
                }
            }
            let headerFrame = CGRect(x: 0, y: yOffset, width: self.headerReferenceSize.width, height: self.headerReferenceSize.height)
            self.headerFrames.append(headerFrame)
            var headerAttribute = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withIndexPath: indexPath)
            headerAttribute.frame = headerFrame
            headerAttribute.zIndex = 1000
            self.layoutAttributes.append(headerAttribute)
            yOffset += self.headerReferenceSize.height - 1 // - 1 so that the bottom border of the header and top border of the cell line up
            let sectionFrame = CGRect(x: 0, y: yOffset, width: itemSize.width, height: itemSize.height)
            self.sectionFrames.append(sectionFrame)
            var sectionAttribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
            sectionAttribute.frame = sectionFrame
            self.layoutAttributes.append(sectionAttribute)
            yOffset += itemSize.height
            let footerFrame = CGRect(x: 0, y: yOffset, width: self.footerReferenceSize.width, height: self.footerReferenceSize.height)
            self.footerFrames.append(footerFrame)
            var footerAttribute = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withIndexPath: indexPath)
            footerAttribute.frame = footerFrame
            self.layoutAttributes.append(footerAttribute)
            yOffset += self.minimumLineSpacing + self.footerReferenceSize.height
        }
        self.contentSize = CGSize(width: self.collectionView!.width, height: yOffset)
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        var newAttributes: [UICollectionViewLayoutAttributes] = []
        for attributes in self.layoutAttributes {
            let frame = attributes.frame
            if !CGRectIntersectsRect(frame, CGRect(x: 0, y: rect.origin.y, width: rect.size.width, height: rect.size.height)) {
                continue
            }
            let indexPath = attributes.indexPath
            let section = indexPath.section
            if let kind = attributes.representedElementKind {
                if kind == UICollectionElementKindSectionHeader {
                    var headerFrame = attributes.frame
                    let footerFrame = self.footerFrames[section]
                    if CGRectGetMinY(headerFrame) <= self.collectionView!.contentOffset.y {
                        headerFrame.origin.y = self.collectionView!.contentOffset.y
                    }
                    if CGRectGetMinY(headerFrame) >= CGRectGetMinY(footerFrame) {
                        headerFrame.origin.y = footerFrame.origin.y
                    }
                    attributes.frame = headerFrame
                    self.headerFrames[section] = headerFrame
                } else if kind == UICollectionElementKindSectionFooter {
                    attributes.frame = self.footerFrames[section]
                }
            } else {
                attributes.frame = self.sectionFrames[section]
            }
            newAttributes.append(attributes)
        }
        return newAttributes
    }

    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let frame = self.sectionFrames[indexPath.item]
        let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
        attributes.frame = frame
        return attributes
    }

    override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let attributes = super.layoutAttributesForSupplementaryViewOfKind(elementKind, atIndexPath: indexPath)
        if elementKind == UICollectionElementKindSectionHeader {
            attributes.frame = self.headerFrames[indexPath.section]
        } else if elementKind == UICollectionElementKindSectionFooter {
            attributes.frame = self.footerFrames[indexPath.section]
        }
        return attributes
    }

    override func collectionViewContentSize() -> CGSize {
        return self.contentSize
    }

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }
}

我建议使用UICollectionViewDelegateFlowLayout方法,以便您可以动态确定标头的参考大小。

相关问题