2009-12-12 95 views
2

這是處理:我有一個UITableView,有兩個部分,當第一個部分爲空時我想顯示一個「無數據」單元,以便2個部分標題爲不要粘在一起(導致它看起來很奇怪)。當部分爲空時,UITableView的背景顏色變爲白色

它的效果很好(即使我在開始工作時遇到麻煩,see this thread)。我使用viewForFooterInSection:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 

if(section == 0) 
{ 
     if([firstSectionArray count] == 0) 
       return 40; 
     else 
       return 0; 
} 

return 0; 

} 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ 

    if(section == 0) 
    { 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 44)]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textColor = [UIColor colorWithWhite:0.6 alpha:1.0]; 
     label.textAlignment = UITextAlignmentCenter; 
     label.lineBreakMode = UILineBreakModeWordWrap; 
     label.numberOfLines = 0; 
     label.text = @"No row"; 
     return [label autorelease]; 
    } 

    return nil; 
} 

但底色變成純白色的,當我顯示部分頁腳視圖。見圖片:

alt text http://img683.yfrog.com/img683/9480/uitableviewproblem.png

我比較喜歡當背景填充空單元格。有沒有人有任何想法如何做到這一點?謝謝

回答

2

當你沒有頁腳時,背景填充空單元格。因此,不要實施viewForFooterInSection(或titleForFooterInSection)方法,您將獲得「空單元格」效果。

我建議您返回,表明沒有條目顯示,像這樣的小區:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (matches.count>0) { 
     // Do your usual thing here 
    } else { 
     static NSString *cellId = @"noDataCell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease]; 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
      cell.textLabel.textColor = [UIColor grayColor]; 
     } 
     cell.textLabel.text = @"Aucun match"; 
     return cell; 
    } 
} 

當然,你必須告訴UIKit中,你總是有至少一個細胞在你的部分...我已經添加了isDeletingRow案件,似乎麻煩你(評論)。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section==0) return matches.count>0 ? matches.count : (isDeletingRow ? 0 : 1); 
    // Set 'isDeletingRow' to YES when a delete is being committed to the table, in that case we let UIKit know that we indeed took care of the delete... 
    // And cover the other sections too... 
} 

當你所提交的修改,您需要設置isDeletingRownumberOfRowsInSection返回一個滿意的價值...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     isDeletingRow = YES; 
     // Your current code when a row is deleted here... 
     isDeletingRow = NO; 
     if (matches.count==0) [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.5]; 
    } 
} 
+0

我已經試過了,但我得到當我刪除最後一個NSInternalInconsistencyException該部分的行:'無效更新:部分0中的行數無效。在更新(1)之後,現有部分中包含的行數必須等於更新之前該部分中包含的行數(1) ,加上或減去從該部分插入或刪除的行數(0插入,1刪除)。' 這讓我瘋狂! – nmondollot 2009-12-13 10:34:30

+0

我已經更新了答案,向你展示瞭如何處理這個問題:)你確實需要去該行的行數爲0,但是還有一些工作要做...... – 2009-12-13 21:28:53

+0

謝謝你多卓然!有用! (儘管仍然有點黑客,但我想我們在這裏沒有選擇...) – nmondollot 2009-12-14 15:22:32