2011-01-30 32 views
9

Ứng dụng được đề cập có khả năng cho người dùng đánh dấu các mục là yêu thích. Khi người dùng không có mục yêu thích được lưu, tôi muốn thông báo cho họ về thực tế (chủ yếu là tôi ghét ý tưởng của một tableView trống).Hiển thị nhãn tùy chỉnh khi không có sẵn dữ liệu cho tableView

My numberOfRowsInSection là 0 khi không có mục nào mà người dùng đã đánh dấu là yêu thích. Tôi muốn đặt cell.textLabel.text = @ "Bạn không có mục yêu thích" nhưng khi không có mục nào cho bảng cellForRowAtIndexPath không được gọi.

Tôi có thể kiểm tra numberOfRowsInSection để đưa ra kết quả khi nó gặp 0 và sau đó kiểm tra 1 hàng trong cellForRowAtIndexPath rồi chèn văn bản tùy chỉnh nhưng sau đó nếu chúng chỉ có một yêu thích?

CẬP NHẬT

tôi đã cố gắng thực hiện các ý tưởng tôi đã nói trên, và khuyến cáo dưới đây, nhưng có lẽ tôi sẽ không làm điều đó đúng, có lẽ là do thực tế rằng đó là một fetchedResultsController với các phương pháp đại biểu để cập nhật bảng khi một sự thay đổi xảy ra.

tôi nhận được lỗi này khi tôi xóa các tế bào khi có nhưng một tế bào trong bảng:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976 Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. 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 (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). with userInfo (null)

và khi có được không các tế bào sẽ được hiển thị ở nơi đầu tiên nó bị treo với:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFBatchFaultingArray objectAtIndex:]: index (0) beyond bounds (0)'

đây là mã có liên quan:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; 

    if ([sectionInfo numberOfObjects]==0) { 
     return 1; 
    } else { 
     return [sectionInfo numberOfObjects]; 
    } 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

if ([[_fetchedResultsController sections] objectAtIndex:0]==0) { 
    cell.textLabel.text = @"You have no favorites"; 
} else { 
    Shows *show = [_fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.textLabel.text = show.ShowsToSerials.serialName; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", show.showTitle]; 
} 
} 

Trả lời

30

1) Trong ứng dụng iOS của riêng tôi, tôi tạo một chế độ xem phụ có nhãn UILabel trong đó, nói "Không có kết quả" Khi numberOfSectionsInTableView bằng không, tôi thêm phần phụ vào bảng. Khi nó được! = Zero, tôi gỡ bỏ nó. Điều này tất nhiên đòi hỏi phải duy trì subview trong bộ nhớ như một đối tượng giữ lại, để bạn có thể loại bỏ nó.

2) Nếu bạn muốn tạo một ô như bạn đã đề cập một cách: Khi numberOfSectionsInTableView bằng 0, trả về 1. Sau đó, trong numberOfRowsInSection cũng trả về 1. Trong cellForRowAtIndexPath, kiểm tra cả numberOfSectionsInTableView và numberOfRowsInSection, và nếu chúng NÊN là số không (sau khi tất cả bạn trả về 1), sau đó hiển thị bất kỳ thông báo nào.

Giải pháp 1 yêu cầu duy trì một biến để giữ subview, nhưng là ngắn hơn và mã sạch hơn, và tôi tin là ít CPU chuyên sâu (không phải là một trong hai sẽ gây ra bất kỳ vấn đề hiệu suất đáng kể).

Chỉnh sửa để bao gồm mã: Nó thực sự hơi khác so với tôi nghĩ, nhưng về cơ bản giống nhau. Có những điều rõ ràng ở đây sẽ được thay đổi để phù hợp với mã và khẩu vị của bạn.

(Hãy chắc chắn rằng tuyên bố UIView * nomatchesView; trong .h)

- (void)viewDidLoad{ 

     nomatchesView = [[UIView alloc] initWithFrame:self.view.frame]; 
     nomatchesView.backgroundColor = [UIColor clearColor]; 

     UILabel *matchesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,320)]; 
     matchesLabel.font = [UIFont boldSystemFontOfSize:18]; 
     matchesLabel.minimumFontSize = 12.0f; 
     matchesLabel.numberOfLines = 1; 
     matchesLabel.lineBreakMode = UILineBreakModeWordWrap; 
     matchesLabel.shadowColor = [UIColor lightTextColor]; 
     matchesLabel.textColor = [UIColor darkGrayColor]; 
     matchesLabel.shadowOffset = CGSizeMake(0, 1); 
     matchesLabel.backgroundColor = [UIColor clearColor]; 
     matchesLabel.textAlignment = UITextAlignmentCenter; 

     //Here is the text for when there are no results 
     matchesLabel.text = @"No Matches"; 


     nomatchesView.hidden = YES; 
     [nomatchesView addSubview:matchesLabel]; 
     [self.tableView insertSubview:nomatchesView belowSubview:self.tableView]; 
    } 



    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     //If there is no table data, unhide the "No matches" view 
     if([data count] == 0){ 
     nomatchesView.hidden = NO; 
     } else { 
     nomatchesView.hidden = YES; 
     } 

     return [data count]; 
    } 
+1

Bạn có vui lòng gửi mã cho giải pháp của bạn 1 không? –

+0

Daniel. Cảm ơn bạn một lần nữa cho mã của bạn. Chỉ là một câu hỏi về quản lý bộ nhớ. Tôi phát hành quan điểm trong dealloc (là tốt nhất?) Nhưng tôi nên phát hành nhãn ở đâu? –

+0

Tôi có chế độ xem và tất cả được phát hành trong dealloc. Tôi không tin bất cứ điều gì đang rò rỉ cho tôi. –

9

Điều mà cá nhân tôi sẽ làm là như sau. Như bạn đã nói, trong phương thức numberOfRowsInSection bạn nên kiểm tra số đếm nguồn dữ liệu tableView của bạn, nếu 0 của nó, bạn nên trả về 1 để hiển thị 1 ô mà bạn muốn.

Sau đó, trong cellForRowAtIndexPath bạn nên kiểm tra số lần nữa, nếu nó trả về 0 bạn biết rằng chế độ xem bảng đang cố vẽ "Bạn hiện không có mục yêu thích" và bạn có thể đặt văn bản.

+1

Đây cũng là một gợi ý tuyệt vời. –

5

Như đã đề cập ở bên dưới liên kết nó rất dễ dàng nếu chúng ta tạo ra một nhãn và thiết lập đó là quan điểm nền cho UITableView như phía dưới. http://www.appcoda.com/pull-to-refresh-uitableview-empty/

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

     self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     return 1; 

    } else { 

     // Display a message when the table is empty 
     UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 

     messageLabel.text = @"No data is currently available. Please pull down to refresh."; 
     messageLabel.textColor = [UIColor blackColor]; 
     messageLabel.numberOfLines = 0; 
     messageLabel.textAlignment = NSTextAlignmentCenter; 
     messageLabel.font = [UIFont fontWithName:@"Palatino-Italic" size:20]; 
     [messageLabel sizeToFit]; 

     self.tableView.backgroundView = messageLabel; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    } 

    return 0; 
} 
+0

Cảm ơn bạn. Tốt rồi. – Raja

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