2012-03-08 18 views
8

Tôi muốn loại bỏ tiêu đề phần khỏi UITableView nếu không có hàng nào cho phần đó.Xóa các phần không có hàng từ UITableView

Tôi đang sử dụng UILocalizedIndexedCollation cho tiêu đề phần của tôi. Vì vậy, khi tôi tạo tiêu đề, tôi không nhất thiết phải biết phần nào sẽ có nội dung.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    //return [customerSections count]; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return 1; 
    } 
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //NSLog(@"Section: %i", section); 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return self.filteredCustomers.count; 
    } else { 
     return [[self.customerData objectAtIndex:section] count]; 
    } 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    // The header for the section is the region name -- get this from the region at the section index. 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return nil;//@"Results"; 
    } 
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section]; 
} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    //return [customerSections allKeys]; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return nil; 
    } 
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; 
} 

Trả lời

1

Tôi đã loại bỏ phần phụ đề chưa sử dụng và tạo các phần dựa trên đó.

Trong số NSURLConnection requestDidFinish Tôi đã sử dụng các mục sau.

self.customerData = [self partitionObjects:[self customers] collationStringSelector:@selector(self)]; 

sau đó đã

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector 
{ 
    sectionIndexTitles = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]]; 
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; 
    NSInteger sectionCount = [[collation sectionTitles] count]; 
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount]; 

    for (int i = 0; i < sectionCount; i++) { 
     [unsortedSections addObject:[NSMutableArray array]]; 
    } 

    for (id object in array) { 
     NSInteger index = [collation sectionForObject:[object objectForKey:@"name"] collationStringSelector:selector]; 
     [[unsortedSections objectAtIndex:index] addObject:object]; 
    } 

    NSMutableArray *sections = [NSMutableArray array]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 

    NSUInteger lastIndex = 0; 
    NSMutableIndexSet *sectionsToRemove = [NSMutableIndexSet indexSet]; 
    for (NSArray *section in unsortedSections) { 
     if ([section count] == 0) { 
      NSRange range = NSMakeRange(lastIndex, [unsortedSections count] - lastIndex); 
      [sectionsToRemove addIndex:[unsortedSections indexOfObject:section inRange:range]]; 
      lastIndex = [sectionsToRemove lastIndex] + 1; 

     } else { 
      NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors]; 
      [sections addObject:sortedArray]; 
     } 
    } 

    [sectionIndexTitles removeObjectsAtIndexes:sectionsToRemove]; 

    return sections; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 

    if (self.searchDisplayController.active) { 
     return 1; 
    } 
    return [sectionIndexTitles count];//[[[UILocalizedIndexedCollation currentCollation] sectionTitles] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if (self.searchDisplayController.active) { 
     return self.filteredCustomers.count; 
    } else { 
     return [[self.customerData objectAtIndex:section] count]; 
    } 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    if (self.searchDisplayController.active) { 
     return nil; 
    } 

    return [sectionIndexTitles objectAtIndex:section]; 
} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 

    if (self.searchDisplayController.active) { 
     return nil; 
    } 

    return sectionIndexTitles; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    return index;//[[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; 
} 

Và với tất cả các mã trên này loại bỏ các chữ cái không sử dụng từ phía bên phải của màn hình cũng như các tiêu đề mục mà không có hàng.

2

Đây là một câu hỏi thú vị với một số giải pháp khả thi.

Làm việc ngược, numberOfSectionsinTableViewnumberOfRowsInSection là những gì cần phải được cập nhật để hiển thị đúng số phần. Đây là một phần phụ thuộc vào phương pháp UILocalizedIndexedCollation phương pháp.

(Có thể điều này xảy ra sau một số hành động của người dùng (xóa hoặc chèn), vì vậy hãy lưu ý rằng trong commitEditingStyle bạn nên gọi [self.tableView reloadData).

Tôi giả định rằng customerData là một mảng, trong đó tại mỗi chỉ mục có một mảng có thể thay đổi tương ứng với một phần. Khi mảng tại một chỉ số customerData nhất định không có dữ liệu, bạn muốn loại bỏ phần đó cho chỉ mục đó.

Giải pháp sau đó, là tính mọi thứ theo cách thủ công - xác định thông tin phần dựa trên cuộc sống bên trong mảng customerData của bạn. Tôi đã đâm vào viết lại ba phương pháp của bạn. Chúc may mắn!

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return nil; 
    } 
    NSMutableArray *arrayToFilter = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 

    //This is the key - recalculate index titles based on what's present in customerData 
    for (int i = [self.customerData count] -1; i >=0 ; i--) { 
     if (![[self.customerData objectAtIndex:i] count]) { 
      [self.arrayToFilter removeObjectAtIndex:i]; 
     } 
    } 
    return arrayToFilter; 
} 

//You need to be calculating your table properties based on the number of objects 
//rather of the 'alphabet' being used. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    //return [customerSections count]; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return 1; 
    } 
    //return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count]; 
    int populatedArrayCounter = 0;   
    for (int i = 0; i <[self.customerData count]; i++) { 
     if ([[self.customerData objectAtIndex:i] count]) { 
      populatedArrayCounter++; 
     } 
    } 
    return populatedArrayCounter; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //NSLog(@"Section: %i", section); 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return self.filteredCustomers.count; 
    } else { 
     // Your original line requires changing, because there are customerData array objects with a count of 0, and you don't want any sections like that 
     // Thus, pick from the set of populated arrays. 
     NSMutableArray populatedArrays = [[NSMutableArray alloc] init];   
     for (int i = 0; i <[self.customerData count]; i++) { 
      if ([[self.customerData objectAtIndex:i] count]) { 
       [populatedArrays addObject:i]; 
      } 
     } 
     return [[populatedArrays objectAtIndex:section] count];; 
    } 
} 
3

Tôi vừa mới đạt được điều này với mã này:

Đây là hàm trả về tiêu đề của phần này, nếu không có hàng trong phần đó thì đừng trả về một danh hiệu:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if ([self.customerData count] > 0) { 
      return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section]; 
     } 
    return nil; 
} 
10

Chỉ muốn kêu vang trong và đưa ra giải pháp của tôi để này

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if ([self.myTableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { 
     return nil; 
    } 
    return [[self.collation sectionTitles] objectAtIndex:section]; 

} 

based on this answer

+1

Mặc dù tôi đã kết thúc tháo sectionIndexTitles cũng này trả lời câu hỏi tốt hơn so với câu trả lời ở trên. – Bot

0

tôi muốn chia sẻ giải pháp của tôi trong nhanh chóng

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if self.sections[section].isEmpty 
    { 
     return nil 
    } 
    else 
    { 
     return collation.sectionTitles[section] 
    } 
} 
Các vấn đề liên quan