2015-04-04 58 views
1

我试图隐藏并显示节标题时有或没有数据填充tableview。我现在的代码偶尔会按预期工作,但大多数情况下,一个或两个节标题将保持显示,(但是,如果我在没有数据时将标题拖出屏幕,它将按预期消失)。动态隐藏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,这似乎并没有被工作虽然。任何想法我应该怎么做呢?谢谢。

+0

我有投票urs希望能回到我的回答 – 2015-06-20 14:34:06

回答

0

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

- (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 ; 


    } 


} 


} 
+0

我有两个自定义标题部分。一个标题为“用户”,一个标题为“歌曲”。我有一个搜索来填充表视图。简单地说,我希望当没有数据填充tableview(坏搜索,初始加载)时隐藏部分标题。 – Andy 2015-04-04 17:05:28

+0

为什么不使用UITableViewCell。而不是检查是否(se​​lf.searchUsers.count || self.searchTracks.count) {}两次尝试使用它一次,当它为空尝试使用具有透明背景的单元格 – 2015-04-04 17:19:16

+0

我正在制作表格视图标题..标题需要UIViews我没有问题显示每个部分的单元格。 – Andy 2015-04-04 17:55:38

0

您的模型应该驱动您的表格视图。不要仅仅从模型中返回“行数”值,而是返回“段数”值。请考虑一个包含两个数组作为模型的数组。

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; 
} 
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方法,这样你就可以动态地确定头部的参考尺寸。