2011-01-30 32 views
9

有問題的應用程序有能力讓用戶將項目標記爲收藏夾。當用戶沒有保存收藏夾時,我想通知他們這個事實(主要是我討厭空白tableView的想法)。當沒有數據可用於tableView時顯示自定義標籤

我的numberOfRowsInSection在沒有用戶標記爲收藏的項目時爲零。我想設置cell.textLabel.text = @「你沒有收藏」,但是當沒有項目的表cellForRowAtIndexPath沒有被調用。

我可以測試numberOfRowsInSection當遇到0時給出結果,然後在cellForRowAtIndexPath中測試1行然後插入自定義文本,但是如果它們只有一個最喜歡的呢?

UPDATE

我試圖執行的想法我上面有,以下推薦的,但也許我不這樣做是正確的,可能是由於這樣的事實,這是一個fetchedResultsController與委託方法時更新表發生變化。

我得到這個錯誤,當我刪除小區時,有在表中,但一個小區:

*** 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)

,並在沒有顯示在它崩潰首位細胞:

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

下面是相關代碼:

- (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]; 
} 
} 

回答

30

1)在我自己的iOS應用程序中,我創建了一個UILabel的子視圖,並說「沒有結果」 當numberOfSectionsInTableView爲零時,我將子視圖添加到tableview中。當它是!=零時,我將它刪除。 這當然需要在內存中維護子視圖作爲保留對象,以便您可以將其刪除。

2)如果您想要製作一個單元格,請稍微提及一下: 當numberOfSectionsInTableView爲零時,返回1.然後,在numberOfRowsInSection中也返回1。 在cellForRowAtIndexPath中,同時檢查numberOfSectionsInTableView和numberOfRowsInSection,如果他們應該是零(畢竟你返回1),然後顯示任何消息。

解決方案1需要維護一個變量來保存子視圖,但代碼更短,代碼更簡潔,我相信CPU佔用更少(不會導致任何重大性能問題)。

編輯包括代碼: 它實際上有點不同於我想的,但基本相同。 這裏顯然有些東西需要改變以符合您的代碼和品味。

(確保申報的UIView * nomatchesView;在.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

您是否願意爲您的解決方案1發佈代碼? – 2011-01-30 02:35:25

+0

丹尼爾。再次感謝您的代碼。只是一個關於內存管理的問題。我在dealloc中釋放視圖(是最好的?),但我應該在哪裏發佈標籤? – 2011-06-09 01:25:48

9

我個人會做的是以下幾點。 正如你自己所說,在numberOfRowsInSection方法中,你應該檢查你的tableView數據源的數量,如果它的0你應該返回1,以顯示你想要的單元格。

然後在cellForRowAtIndexPath中,您應該再次檢查計數,如果它返回0您知道表視圖試圖繪製您的「您目前沒有收藏夾」,您可以設置文本。

+1

這也是一個很好的建議。 – 2011-06-09 01:26:50

5

正如下面提到鏈接很容易,如果我們創建了一個標籤,並設置爲喜歡的UITableView背景視圖下面。 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; 
} 
相關問題