2009-06-20 32 views
21

如果你有一個普通的UITableView(沒有分組),只有一行,屏幕的其餘部分填充空白或空單元格。你如何改變這些空白單元格的外觀?首先,我認爲他們會在表格視圖中使用單元格的外觀,但似乎他們沒有。如何修改普通UITableView中'空'單元的外觀?

來自Cultured Code(Things)的人對修改這些單元格做了很好的工作,但我無法立即想到改變外觀的方法。

任何提示?

+0

*凹凸*得到了有效的解決方案?請在此分享。謝謝 – lostInTransit 2009-06-22 08:17:39

+0

我迄今爲止所做的是在表視圖的末尾添加一個'不可見'單元格,並使用imageview來顯示錶視圖結束的陰影。我已將分隔符樣式設置爲無。這產生了類似的效果,但我不得不稍微調整一下以獲得滿意的效果。 – 2009-06-22 10:34:13

回答

20

儘管這不是你看上去很什麼,你也可以將其更改爲只通過設置在桌子上一個頁腳視圖顯示一個空白區(而不是空白細胞)。一個高度爲0的UIView將完成這個技巧。

+1

在你的幫助下,我發現它很簡單。將所需的UIImageView添加到UITableView的頁腳(如您所建議的那樣),但要使表視圖正確運行,必須使用UIImageView的高度增加其高度。這樣,表格視圖就像普通的表格視圖一樣工作,但是你有很好的效果。一定要刪除表視圖的單元格分隔符。 – 2009-06-24 00:56:22

+0

你有這個工作的任何代碼?我有困難重現這裏解釋 – 2012-11-19 15:26:05

0

您可能必須製作UITableView的自定義子類,以填充空白單元格的外觀填充背景。

- (void)drawRect:(CGRect)rect { 
    [super drawRect: rect]; 

    // draw your background here if the rect intersects with the background area 
} 
5

我設置了一個〜300px高UIView子類到我的tableView頁眉和頁腳視圖,調整tableView插圖,以補償這些視圖(設置頂部和底部的插圖-300px)。

我UIView子類實現drawRect方法中,我使用CGContextDrawTiledImage()繪製空UITableViewCell重複:

- (void)drawRect:(CGRect)rect { 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 46),NO,0.0); 
    emptyCell = [[SWTableViewCell alloc] initWithFrame:CGRectZero]; 
    [emptyCell drawRect:CGRectMake(0, 0, 300, 46)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    [emptyCell release]; 
    UIGraphicsEndImageContext(); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextScaleCTM (context, 1, -1); // this prevents the image from getting drawn upside-down 
    CGContextDrawTiledImage(context, CGRectMake(0, 0, 300, 46), [newImage CGImage]); 
} 

在我的情況我tableviewcells是46px高,所以如果我想使UIView子類含有8這些空單元格,我需要使它高達368px。

7

基於samvermette's答案,但被修改爲直接使用背景圖像,而不是從UITableViewCell子類合成圖像。山姆的答案很好,但與直接創建背景圖片相比,效果會更差。

創建UIView子類 - 在這個例子中,我把它叫做CustomTiledView - 和下面的代碼添加到類:

- (void)drawRect:(CGRect)rect { 
UIImage *image = [UIImage imageNamed:@"tableview_empty_cell_image"]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextScaleCTM (context, 1, -1); 
CGContextDrawTiledImage(context, 
         CGRectMake(0, 0, rect.size.width, image.size.height), 
         [image CGImage]); 
} 

下面的代碼添加到您的tableviewcontroller:

- (CGFloat)tableView:(UITableView *)tableView 
      heightForFooterInSection:(NSInteger)section { 
    // this can be any size, but it should be 1) multiple of the 
    // background image height so the last empty cell drawn 
    // is not cut off, 2) tall enough that footer cells 
    // cover the entire tableview height when the tableview 
    // is empty, 3) tall enough that pulling up on an empty 
    // tableview does not reveal the background. 
     return BACKGROUND_IMAGE_HEIGHT * 9; // create 9 empty cells 
} 

- (UIView *)tableView:(UITableView *)tableView 
      viewForFooterInSection:(NSInteger)section { 
    CustomTiledView *footerView = [[CustomTiledView alloc] init]; 
    return [footerView autorelease]; 
} 

最後,您需要將tableview的底部內容設置爲從-tableView:heightForFooterInsection:返回的值的負數。在此示例中,它將是-1*BACKGROUND_IMAGE_HEIGHT*9。您可以通過Interface Builder的Size Inspector設置底部內容,也可以通過設置tableviewcontroller的self.tableView.contentInset屬性來設置底部內容。

乾杯!

0

我相信使用UITableViewtableView:viewForFooterInSection:方法的方法不適用於表爲空的情況,即沒有單元格的情況。在這種情況下,你可以使用Answerbot提出可以重新用於創建UIView塞進表的頁腳觀點明確,像下面的技術:

CGRect cellRect = CGRectMake(0, 0, table.bounds.size.width, table.bounds.size.height); 
    CustomTiledView *footerView = [[[CustomTiledView alloc] initWithFrame:cellRect] autorelease]; 
    table.tableFooterView = footerView; 
1

如果你只是想改變「空單元格的高度「當你的TableView中沒有任何單元時:你需要設置你的UITableView的rowHeight屬性(tableView:heightForRowAtIndexPath:的實現對空的TableViews沒有影響)。如果您的TableView中有單元格並且已實施tableView:heightForRowAtIndexPath:,則最後一行高度將用於空單元格。

3

http://jomnius.blogspot.com/2011/03/hide-uitableview-empty-cell-separator.html 簡單的方法是定義表沒有細胞分離機行:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

艱辛的道路,如果你需要分割線,是定義表沒有分割線,創造細胞分隔線作爲自定義UITableViewCell的一部分。很明顯,單元格的底部部分很可能使用了非常薄的圖形圖像。記住要正確定義圖像自動調整和模式。

另一種方法是定義一個空的UITableView的tableFooterView:

UIView *footer = 
    [[UIView alloc] initWithFrame:CGRectZero]; 
self.myTable.tableFooterView = footer; 
[footer release]; 
相關問題