2010-02-28 44 views
5

Tôi gặp sự cố khi tạo hoạt ảnh cho tiêu đề phần UITableView tùy chỉnh.
Mục tiêu là tạo các phần có thể thu gọn.
Khi tôi nhấn vào tiêu đề tùy chỉnh lần đầu tiên nó hoạt ảnh như mong đợi, tuy nhiên mỗi lần sau đó nó để lại một bản sao ở vị trí ban đầu và hoạt ảnh khác.Tiêu đề phần tùy chỉnh UITableView, vấn đề trùng lặp

Hình ảnh Ví dụ:

alt text http://img35.imageshack.us/img35/4018/screenshot2010022722565.png alt text http://img291.imageshack.us/img291/8287/screenshot2010022723035.png
Tuỳ chỉnh Tiêu đề của tôi:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
     UIView* customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease]; 
     customView.backgroundColor = [UIColor lightGrayColor]; 

     UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
      headerLabel.backgroundColor = [UIColor clearColor]; 
      headerLabel.opaque = NO; 
      headerLabel.textColor = [UIColor darkGrayColor]; 
      headerLabel.font = [UIFont boldSystemFontOfSize:16]; 
      headerLabel.frame = CGRectMake(10, 7, 260.0, 44.0); 
      headerLabel.textAlignment = UITextAlignmentCenter; 
      NSDictionary *dictionary = [self.data objectAtIndex:section]; 
      headerLabel.text = [dictionary objectForKey:@"Title"]; 

      [customView addSubview:headerLabel]; 


      // add button to right corner of section 
     UIButton* headerButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 320, 44)]; 
      headerButton.center = CGPointMake(160.0, 22.0); 
      headerButton.backgroundColor = [UIColor clearColor]; 
      headerButton.tag = section; 
      [headerButton addTarget:self action:@selector(expandSection:) forControlEvents:UIControlEventTouchUpInside]; 

      [customView addSubview:headerButton]; 

      return customView; 
} 

My Animation Phương pháp:

- (void) expandSection:(id)sender { 

    if (expandedSection == [sender tag]) { 
     expandedSection = -1; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else if (expandedSection == -1){ 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else{ 
     [self.tableView beginUpdates]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone]; 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView endUpdates]; 

    } 
    //[self.tableView reloadData]; 
} 

Tôi không chắc chắn chính xác whats going on, nhưng các trường hợp cho thấy rằng tôi cần phải giải quyết một cái gì đó. Tôi đã thử một vài điều nhưng tôi không thể hiểu được điều này. Bất cứ ai giúp đỡ về điều này sẽ là tuyệt vời!

Chỉnh sửa: Tôi tin rằng vấn đề là việc tải lại Mục đang gây ra chế độ xem tùy chỉnh. Tôi không thể phát hành chế độ xem vì tôi cần nó làm tài liệu tham khảo để cập nhật cho hoạt ảnh. Bất kỳ ý tưởng về những gì tôi có thể làm để sửa lỗi này?

Trả lời

8

Tìm thấy giải pháp.

Bảng cần được tải lại trước mỗi thay đổi. Bằng cách này, bảng ở trạng thái mới nhất trước khi thực hiện bất kỳ thay đổi nào.

thêm [self.tableView reloadData]; là mục nhập nắm tay trong phương pháp "expandSection".

Mã sản phẩm:

- (void) expandSection:(id)sender { 

    [self.tableView reloadData]; 

    if (expandedSection == [sender tag]) { 
     expandedSection = -1; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else if (expandedSection == -1){ 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else{ 
     [self.tableView beginUpdates]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone]; 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView endUpdates]; 

    } 
    //[self.tableView reloadData]; 
} 
+0

Cảm ơn bạn đã khắc phục sự cố liên quan nơi tôi muốn tạo hiệu ứng động khi xóa/chèn các hàng giống nhau, trong đó, trước tiên không gọi 'reloadData', nó sẽ chỉ tạo hiệu ứng xóa, nhưng không tạo hiệu ứng chèn. Vấn đề rất lạ. – Jeremy

+0

Cách tốt ... + 1 cho bạn người đàn ông – Sabby

2

Tôi đã có một vấn đề tương tự mà được gây ra bằng cách sử dụng các tế bào chiều cao năng động. Tôi đã có một khung nhìn tiêu đề tùy chỉnh có thể mở rộng và khi tôi đang cập nhật tableView để chèn và loại bỏ các hàng liên quan của phần (có nghĩa là chúng đang mở rộng, tương ứng sụp đổ), tiêu đề phần là phân lớp của UITableViewHeaderFooterView không được tái chế. Vì vậy, về cơ bản một cái mới được phân bổ và thêm vào cái cũ dẫn đến các khung nhìn chồng lên nhau. Số nhận dạng ô được đặt chính xác nên phải có thứ gì đó khác. Khi tôi xóa tableView.sectionHeaderHeight = UITableViewAutomaticDimension và triển khai func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat chế độ xem đã được tái chế đúng cách và chỉ một chế độ xem tiêu đề được hiển thị cho từng phần.

Một giải pháp mà tôi hóa ra để thực sự làm việc là sử dụng UITableViewCell thay vì UITableViewHeaderFooterView và khi bạn quay trở lại trong func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? bạn chỉ return cell.contenView, điều này sẽ làm việc vì phương pháp này đòi hỏi phải trả lại một UIView và kể từ khi contentView của UITableViewCell là một UIView nó hoạt động tốt. Ý tưởng đằng sau là sử dụng cơ chế tái chế của UITableView thông qua UITableViewCell và chỉ trả về nội dung của nó sau khi bạn cấu hình nó.

Kết luận. Vấn đề là rất có thể được gây ra bởi UITableViewHeaderFooterView khi được sử dụng với các ô bảng tự định kích thước và UITableViewAutomaticDimension thay vì tính toán chiều cao ô theo cách thủ công.

+0

Điều thú vị mà tôi đã sử dụng UITableViewCells cho tiêu đề, không sử dụng các ô tự định kích thước và tôi vẫn gặp sự cố ở đây :) – Gobe

Các vấn đề liên quan