2015-04-04 59 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方法,這樣你就可以動態地確定頭部的參考尺寸。