2010-08-16 6 views
5

Tôi có UITableView và về cơ bản tôi đang thực hiện một số cài đặt ứng dụng và nếu phần đầu tiên là UISegmentedControl được chuyển sang chỉ mục 1, thì tôi muốn hiển thị phần mới, nhưng nếu chỉ mục 1 đã được đặt trước đó và người dùng chọn chỉ mục 0 sau đó tôi cần phải loại bỏ phần 2.UITableView thêm/xóa các phần trong khi không ở chế độ chỉnh sửa?

để thực hiện điều này tôi đã có mã này thiết lập để bắn vào các UISegmentedControl's valueChanged event

if (segmentControl.selectedSegmentIndex == 0) 
{ 
    self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) { 

     NSArray *indexSections = [NSArray arrayWithObjects: 
      [NSIndexPath indexPathForRow:0 inSection: 
       [sections indexOfObject:FT_AND_IN]], 
      [NSIndexPath indexPathForRow:0 inSection: 
       [sections indexOfObject:FRACTION_PRECISION]], nil]; 
     [sections removeObject:FT_AND_IN]; 
     [sections removeObject:FRACTION_PRECISION]; 
     [self.tableView deleteRowsAtIndexPaths:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else { 
    self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
    [sections insertObject:FRACTION_PRECISION atIndex:2]; 
    NSArray *indexSections = [NSArray arrayWithObjects: 
     [NSIndexPath indexPathForRow:0 inSection: 
      [sections indexOfObject:FT_AND_IN]], 
     [NSIndexPath indexPathForRow:0 inSection: 
      [sections indexOfObject:FRACTION_PRECISION]], nil]; 
    [self.tableView insertRowsAtIndexPaths:indexSections 
     withRowAnimation:UITableViewRowAnimationRight]; 
} 

Trường hợp NSMutableArray gọi sections là danh sách của tất cả các bộ phận. Mỗi phần chỉ có 1 hàng nên không cần mảng phụ.

Tuy nhiên khi đánh giá các phần khác tôi nhận được lỗi này:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], 
    /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:904 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
    reason: 'Invalid update: invalid number of sections. The number of sections 
    contained in the table view after the update (6) must be equal to the number of 
    sections contained in the table view before the update (4), plus or minus the 
    number of sections inserted or deleted (0 inserted, 0 deleted).' 

tôi đã xác minh rằng nó có 4 phần trước khác, nó thêm vào một cách chính xác hai phần đến các mảng sections, tôi nói với nó các indexPath thích hợp cho các phần được thêm vào. Tại sao nó không hoạt động?

Tôi đã thử thay thế đường dây [self.tableView insertRows/deleteRows...] bằng [self.tableView reloadData]; và sau đó nó hoạt động tốt, nhưng tôi muốn tạo hiệu ứng thêm/xóa các phần đó.

Cập nhật tôi đã cố gắng đề nghị này và các công trình bổ sung nhưng tôi nhận được đâm vào việc loại bỏ

[self.tableView beginUpdates]; 
if (segmentControl.selectedSegmentIndex == 0) 
{ 
     self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && 
      [sections containsObject:FRACTION_PRECISION]) 
     { 

     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: 
       [sections indexOfObject:FT_AND_IN]] 
       withRowAnimation:UITableViewRowAnimationRight]; 
     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: 
       [sections indexOfObject:FRACTION_PRECISION]] 
       withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else 
    { 
     self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
     [sections insertObject:FRACTION_PRECISION atIndex:2]; 
     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1]; 
    NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndex:2]; 
    [self.tableView insertSections:indexSet 
      withRowAnimation:UITableViewRowAnimationRight]; 
    [self.tableView insertSections:indexSet2 
      withRowAnimation:UITableViewRowAnimationRight]; 
} 
[self.tableView endUpdates]; 

tôi nhận được lỗi này.

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** - 
    [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds 
    maximum index value of NSNotFound - 1' 

Các FT_AND_INFRACTION_PRECISION đối tượng chỉ được thêm/xóa khỏi cửa hàng dữ liệu trong mã này, và họ chỉ là const NSString đối tượng.

+0

Là buồn khi thấy một người có điểm số cao không định dạng mã – vodkhang

+0

Đây là vụ phải hoạt động tốt. Tôi không thể thấy bất kỳ vấn đề gì. Bạn có thể kiểm tra lại mảng indexSections không? – vodkhang

Trả lời

4

Rất khó để đọc mã chưa được định dạng của bạn ở đó.

Bạn muốn xem -[UITableView insertSections:withRowAnimation:]-[UITableView deleteSections:withRowAnimation], tôi nghĩ vậy.

Hãy thử:

if (segmentControl.selectedSegmentIndex == 0) 
{ 
    self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) { 

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]]; 
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]]; 

     [sections removeObject:FT_AND_IN]; 
     [sections removeObject:FRACTION_PRECISION]; 

     [self.tableView deleteSections:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else { 
    self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
    [sections insertObject:FRACTION_PRECISION atIndex:2]; 

    NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]]; 
    [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]]; 

    [self.tableView insertSections:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
} 
+0

Tôi vẫn không chắc chắn tại sao phiên bản đã chỉnh sửa của myne không hoạt động, nhưng bản của bạn thì không. Vấn đề duy nhất với bạn là dòng '[self.tableView addSections ...]' phải là 'insertSections'. Cảm ơn! – jamone