2011-11-29 33 views
34

Tôi muốn tải lại chỉ một phần không phải toàn bộ bảng. Có phương pháp nào trong UITableView không.Tải lại phần UITableView

[tableView reloadData] được sử dụng để tải toàn bộ bảng.
Tôi muốn biết cách tải chỉ một phần, vì tôi có số hàng lớn trong bảng.

+2

Calling reloadData vẫn chỉ ban đầu sẽ tải lại UITableViewCells có trên màn hình –

+0

trùng lặp có thể xảy ra http://stackoverflow.com/ câu hỏi/3485162/tải lại-chỉ-một-uitableviewcell-on-a-uitableview –

+0

thanx steve việc làm cho ur giúp –

Trả lời

30

Vâng, đó là:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 
2

Bạn cần điều này ... Đối với Nạp lại Row

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

hoặc Đối Nạp lại phần

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 
66

Các lỗi phương pháp reloadSections tôi - vì tôi phải xây dựng một vài vật thể. Điều này là tuyệt vời nếu bạn cần sự linh hoạt, nhưng đôi khi tôi cũng chỉ muốn sự đơn giản quá. Nó như sau:

NSRange range = NSMakeRange(0, 1); 
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];          
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone]; 

Điều này sẽ tải lại phần đầu tiên. Tôi thích có một danh mục trên UITableView và chỉ cần gọi phương pháp này:

[self.tableView reloadSectionDU:0 withRowAnimation:UITableViewRowAnimationNone]; 

phương pháp loại của tôi trông như thế này:

@implementation UITableView (DUExtensions) 

- (void) reloadSectionDU:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation { 
    NSRange range = NSMakeRange(section, 1); 
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];          
    [self reloadSections:sectionToReload withRowAnimation:rowAnimation]; 
} 
+1

Yêu các chức năng @implementation! Cảm ơn bạn đã làm cho nó đơn giản như vậy – portforwardpodcast

+0

Tôi không thể làm việc này. – Jason

+0

@Jason Bạn đã từng tạo danh mục của riêng mình chưa? Ngoài ra, bạn đã bỏ phiếu xuống? – bandejapaisa

21

Nhưng bạn có thể tải lại chỉ phần mà chứa cùng một số hàng(hoặc bạn phải tự thêm hoặc xóa chúng). Nếu không, bạn sẽ nhận được:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

nào là không cần thiết khi bạn sử dụng [tableView reloadData].

Khi bạn cần phải tải lại một phần và bạn đã thay đổi số hàng bên trong nó, bạn có thể sử dụng một cái gì đó như thế này:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section]; 

[self beginUpdates]; 
    [self deleteSections:indexSet withRowAnimation:rowAnimation]; 
    [self insertSections:indexSet withRowAnimation:rowAnimation]; 
[self endUpdates]; 

Nếu bạn đặt nó trong một thể loại (như chương trình bandejapaisa) nó có thể trông giống như sau:

- (void)reloadSection:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation { 
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section]; 

    [self beginUpdates]; 
     [self deleteSections:indexSet withRowAnimation:rowAnimation]; 
     [self insertSections:indexSet withRowAnimation:rowAnimation]; 
    [self endUpdates]; 
} 
+0

Thật không may điều này không làm việc cho tôi. –

+0

Và trong trường hợp nào? Cách tiếp cận tốt hơn có thể là sử dụng KVO (và do đó nhận được khả năng tải lại UITableView tự động dựa trên các thay đổi đối với một số mô hình) - hoặc nếu bạn sử dụng CoreData, bạn có thể quan tâm đến NSFetchResultsController. – Inza

4

Dựa trên câu trả lời được chấp nhận ở đây, tôi đã thực hiện một chức năng sẽ tải lại tất cả các phần trong bảng bằng hoạt ảnh. Điều này có thể được tối ưu hóa bằng cách tải lại chỉ các phần hiển thị.

[self.tableView reloadData]; 
NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]); 
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; 
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade]; 

Trong trường hợp của tôi, tôi phải buộc tải lạiData trước khi hoạt ảnh của phần, vì dữ liệu cơ bản cho bảng đã thay đổi. Nó hoạt hình đúng cách tuy nhiên.

2

Cố gắng sử dụng

[self.tableView beginUpdates]; 
[self.tableView endUpdates]; 

Hy vọng điều này sẽ giải quyết vấn đề của bạn.

0

Nếu bạn có chế độ xem phần tùy chỉnh, bạn có thể thêm tham chiếu yếu vào nó trong trình điều khiển chế độ xem của bạn và cập nhật nó bất cứ khi nào bạn muốn.Đây là mã của tôi để tham khảo:

@property (weak, nonatomic) UILabel *tableHeaderLabel; 

.... 

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UITableViewHeaderFooterView *myHeader = [[UITableViewHeaderFooterView alloc] init]; 

    UILabel *titleLabel = [[UILabel alloc] init]; 
    [titleLabel setFrame:CGRectMake(20, 0, 280, 20)]; 
    [titleLabel setTextAlignment:NSTextAlignmentRight]; 
    [titleLabel setBackgroundColor:[UIColor clearColor]]; 
    [titleLabel setFont:[UIFont systemFontOfSize:12]]; 

    [myHeader addSubview:titleLabel]; 

    self.tableHeaderLabel = titleLabel; //save reference so we can update the header later 

    return myHeader; 
} 

Sau đó, sau này bạn có thể cập nhật phần của bạn như thế này:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.tableHeaderLabel.text = [NSString stringWithFormat:@"Showing row: %ld", indexPath.row]; 
} 
6

rằng cách chính xác:

[self.tableView beginUpdates]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; 
[self.tableView endUpdates]; 
2

Đây là phương pháp, bạn có thể chuyển chi tiết phần theo các cách khác nhau

[self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:NO]; 

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone]; 

Tải lại các phần cụ thể sẽ cải thiện hiệu suất cho chế độ xem bảng cũng như một số thời gian cũng tránh một số vấn đề như phần đầu trang/chân trang tùy chỉnh nổi/di chuyển trong chế độ xem của bạn. SO cố gắng sử dụng reloadSection hơn relaodData bất cứ khi nào có thể

6

Đối Swift 3 và Swift 4

let sectionToReload = 1 
let indexSet: IndexSet = [sectionToReload] 

self.tableView.reloadSections(indexSet, with: .automatic) 
Các vấn đề liên quan