2013-07-20 55 views
11

tôi đã tìm thấy hướng dẫn này mà giấu một phần của một TableView tĩnh: http://code-ninja.org/blog/2012/02/29/ios-quick-tip-programmatically-hiding-sections-of-a-uitableview-with-static-cells/Hide phần của một TableView tĩnh

Nó hoạt động tuyệt vời nhưng chỉ mà không sửa đổi nó, nếu tôi thêm một phần hoặc một hàng, nó hoạt động xấu . Tôi là người mới bắt đầu và tôi không thể sửa đổi nó, ai đó có thể giúp tôi che giấu nhiều hơn một phần không?

Cảm ơn bạn rất nhiều!

+0

mục đích cố gắng để làm một bảng tĩnh năng động là gì? Đó là những gì các bảng động. –

+0

Ý tưởng của tôi là triển khai nó trong ứng dụng phức tạp, phức tạp hơn và ở đó tôi không thể chuyển đổi mọi thứ thành năng động, tuy nhiên tôi đã giải quyết được (nhiều hơn hoặc ít hơn)! –

Trả lời

32
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    
    if (section == 2 && _hideTableSection) { 
     //header height for selected section 
     return 0.1; 
    } else { 
     //keeps all other Headers unaltered 
     return [super tableView:tableView heightForHeaderInSection:section]; 
    } 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {   
    if (section == 2 && _hideTableSection) { 
     //header height for selected section 
     return 0.1; 
    } else { 
     // keeps all other footers unaltered 
     return [super tableView:tableView heightForFooterInSection:section]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) { //Index number of interested section 
     if (hideTableSection) { 
      return 0; //number of row in section when you click on hide 
     } else { 
      return 2; //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash) 
     } 
    } else { 
     return [super tableView:tableView numberOfRowsInSection:section]; //keeps inalterate all other rows 
    }  
} 
+2

Nếu bảng là một bảng nhóm thì tốt hơn là trả về 0,1 cho chiều cao đầu trang/chân trang. Trả về 0 làm cho UITableView vẫn có một khoảng trống trong đó phần phải là. – datinc

+0

đó là một ý tưởng hay, thường tôi trở lại 1 nhưng 0.1 thậm chí còn tốt hơn !!=) –

1

Thiết lập giá trị phần để 0.01, Dù phần bạn muốn che giấu bạn có thể thử theo cách này: -

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    CGFloat headerHeight=10.f; 
    if (section==0) 
    { 
     headerHeight=0.01f; 
    } 
    else 
    { 
     headerHeight=50.0f; 
    } 
    return headerHeight; 
} 
2

Nếu bạn quay trở lại 0 cho chiều cao của phần này, Apple API sẽ bỏ qua nó. Vì vậy, chỉ cần trả về một giá trị nhỏ hơn 0.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if (section == 0) { 
    return 1; 
    } 

    return 44; 
} 

Cũng thực hiện chế độ xem cho tiêu đề và trả về cho phần bạn không muốn hiển thị.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    if (section == 0 && !self.personaCells.count) { 
    return nil; 
    } 

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)]; 
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, headerView.frame.size.width, 20)]; 
    NSString *headerTitle = @"SAMPLE TITLE"; 
    headerLabel.text = headerTitle;  
    [headerView addSubview:headerLabel]; 
    return headerView; 
} 
12

Tôi muốn chia sẻ một số mã mà tôi đã viết để giải quyết vấn đề này sau khi đào nhiều câu trả lời và gặp nhiều lỗi. Điều này là dành cho xCode 7.2.1. (Ví dụ về mã trong Swift)

Trường hợp sử dụng của tôi là tôi muốn sử dụng tính năng TableView được nhóm tĩnh một cách dễ dàng, nhưng tôi cần ẩn các phần cụ thể dựa trên hồ sơ người dùng. Để thực hiện công việc này (như được mô tả trong các bài viết khác), tôi cần phải ẩn đầu trang và chân trang, các hàng trong phần AND ẩn văn bản đầu trang/chân trang (ít nhất là ở phần trên cùng). Tôi thấy rằng nếu tôi không ẩn (làm trong suốt) văn bản thì người dùng có thể cuộn lên trên đầu bảng (trong Trình điều khiển Điều hướng) và xem văn bản tất cả được nhồi nhét lại với nhau.

Tôi muốn thực hiện điều này dễ dàng để sửa đổi và không muốn điều kiện lây lan qua mã của tôi, vì vậy tôi đã tạo ra một hàm duy nhất gọi là shouldHideSection (phần: Int) là hàm duy nhất tôi phải thay đổi để sửa đổi các hàng nào Đang ẩn.

func shouldHideSection(section: Int) -> Bool { 
    switch section { 
    case 0: // Hide this section based on condition below 
     return user!.isProvider() ? false : true 

    case 2: 
     return someLogicForHiddingSectionThree() ? false : true 

    default: 
     return false 
    } 
} 

Bây giờ phần còn lại của mã chỉ gọi shouldHideSection().

// Hide Header(s) 
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section) 
} 

// Hide footer(s) 
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForFooterInSection: section) 
} 

// Hide rows in hidden sections 
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return shouldHideSection(indexPath.section) ? 0 : super.tableView(tableView, heightForRowAtIndexPath: indexPath) 
} 

// Hide header text by making clear 
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    if shouldHideSection(section) { 
     let headerView = view as! UITableViewHeaderFooterView 
     headerView.textLabel!.textColor = UIColor.clearColor() 
    } 
} 

// Hide footer text by making clear 
override func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
    if shouldHideSection(section) { 
     let footerView = view as! UITableViewHeaderFooterView 
     footerView.textLabel!.textColor = UIColor.clearColor() 
    } 
} 

Tôi đã thử nghiệm với nhiều giá trị khác nhau (trả về 0, 0,1, -1, ...) để có được giải pháp thỏa đáng (ít nhất trên iOS 9.x).

Tôi hy vọng điều này hữu ích, hãy cho tôi biết nếu bạn đã đề xuất cải tiến.

+1

Tôi đã tạo một lớp con của UITableViewController có tên là DynamicUITableViewController. Nó cho phép ẩn các phần/hàng w/o phải thực hiện tất cả các mã này trong TableViewControllers của bạn. Đây là repo github: (https://github.com/tkeithblack/DynamicUITableViewController) – KeithB

+1

Tôi vừa phát hiện ra rằng thay vì trả về -1 khi bạn không muốn thu nhỏ một hàng hoặc tiêu đề nếu bạn gọi super.tableView (...) điều này sau đó sẽ tôn trọng Chiều cao hàng tùy chỉnh được đặt trong bảng phân cảnh. Tôi đã chỉnh sửa ví dụ trên và mục nhập github của tôi để phản ánh thay đổi này. – KeithB

+0

Trong 'table View (_: sẽ hiển thị Header View: for Section:)' và 'table View (_: sẽ hiển thị Footer View: cho Section:) 'bạn chỉ đơn giản có thể làm' view.isHidden = true' thay vì truyền và làm cho 'UILabel' trong suốt. – Gary

0

nếu bạn xóa tiêu đề của phần tiêu đề khỏi bảng phân cảnh, nó sẽ tự động biến mất. Bằng cách đó, ý tôi không chỉ là nội dung tiêu đề, mà còn là không gian của nó.

2

Đối Swift 3

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

    if section == 2 && hideTableSection { 
     //header height for selected section 
     return 0.1 
    } 

    //keeps all other Headers unaltered 
    return super.tableView(tableView, heightForHeaderInSection: section) 
} 

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 

    if section == 2 && hideTableSection { 
     //header height for selected section     
     return 0.1 
    } 

    return super.tableView(tableView, heightForFooterInSection: section) 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if section == 1 { //Index number of interested section 
     if hideTableSection { 
      return 0 //number of row in section when you click on hide 
     } else { 
      return 2 //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash) 
     } 
    } else { 
     return super.tableView(tableView, numberOfRowsInSection: section) //keeps inalterate all other rows 
    } 
} 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    if section == 2 && hideTableSection { 
     return "" 
    } 

    return super.tableView(tableView, titleForHeaderInSection: section) 
} 

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { 

    if section == 2 && hideTableSection { 
     return "" 
    } 

    return super.tableView(tableView, titleForFooterInSection: section) 
}