2011-12-15 18 views
5

Tôi đang cố gắng xóa hoặc ẩn một phần bên trong một chế độ xem bảng với các ô tĩnh trên đó. Tôi đang cố gắng giấu nó trong hàm viewDidLoad. Đây là mã:Cách xóa các phần khỏi ô xem tĩnh

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView beginUpdates]; 
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES]; 
    [self.tableView endUpdates]; 

    [self.tableView reloadData]; 
} 

Vẫn các phần xuất hiện. Tôi đang sử dụng bảng phân cảnh trong đó. Bạn có thể giúp tôi không?

Trả lời

-2

Dường như reloadData làm cho chế độ xem bảng đọc lại nguồn dữ liệu. Bạn cũng nên xóa dữ liệu khỏi dataSource trước khi gọi reloadData. Nếu bạn đang sử dụng mảng, hãy xóa đối tượng bạn muốn với removeObject: trước khi gọi reloadData.

6

Tôi thấy thuận tiện nhất để ẩn các phần bằng cách ghi đè sốOfRowsInSection.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) 
     // Hide this section 
     return 0; 
    else 
     return [super tableView:self.tableView numberOfRowsInSection:section]; 
} 
+0

Điều này phù hợp với mục đích của tôi, nhưng vẫn còn một chút không gian dọc sau khi thực hiện việc này. – guptron

+0

Có chính xác những gì OP muốn, linh hồn tuyệt vời! – Tumtum

0

Thông tin cho bạn đây. Điều này cũng loại bỏ không gian dọc.

NSInteger sectionToRemove = 1; 
CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer 
BOOL removeCondition; // set in viewDidLoad 

/** 
* You cannot remove sections. 
* However, you can set the number of rows in a section to 0, 
* this is the closest you can get. 
*/ 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if (removeCondition && section == sectionToRemove) 
    return 0; 
    else 
    return [super tableView:self.tableView numberOfRowsInSection:section]; 
} 

/** 
* In this case the headers and footers sum up to a longer 
* vertical distance. We do not want that. 
* Use this workaround to prevent this: 
*/ 
- (CGFloat)tableView:(UITableView *)tableView 
    heightForFooterInSection:(NSInteger)section { 

    return removeCondition && 
       (section == sectionToRemove - 1 || section == sectionToRemove) 
      ? distance/2 
      : distance; 
} 

- (CGFloat)tableView:(UITableView *)tableView 
    heightForHeaderInSection:(NSInteger)section { 

    return removeCondition && 
       (section == sectionToRemove || section == sectionToRemove + 1) 
      ? distance/2 
      : distance; 
} 
Các vấn đề liên quan