回答
更新
顯然,現有的UITableViewCell架構使得它很難改變一個單元格的背景顏色,並將它通過其所有的狀態改變(編輯模式等)很好地工作。
解決方案已發佈在this SO question,它在多個論壇上被評爲「Apple工程師批准的唯一解決方案」。它涉及爲UITableViewCell創建子類併爲子類單元的backgroundView
屬性添加自定義視圖。
原帖 - 這個解決方案並不完全工作,但可能仍然是有用的在某些情況下
如果你已經擁有了的UITableViewCell對象,只是改變其contentView
的backgroundColor
財產。
如果您需要使用自定義背景顏色創建UITableViewCells,則該過程稍長一點。首先,你需要爲你的UITableView創建一個數據源 - 這可以是實現UITableViewDataSource
協議的任何對象。
在該對象中,您需要實現tableView:cellForRowAtIndexPath:
方法,該方法在爲表格中單元格的位置提供NSIndexPath時返回UITableViewCell。當您創建該單元格時,您需要更改contentView
的backgroundColor
屬性。
不要忘記將UITableView的dataSource
屬性設置爲您的數據源對象。
欲瞭解更多信息,可以閱讀這些API文檔:
- UITableViewDataSource -
tableView:cellForRowAtIndexPath
- UITableViewCell -
contentView
- UIView -
backgroundColor
- UITableView -
dataSource
注意,註冊成爲蘋果開發者需要所有這三個鏈接。
backgroundView一直在底部。它是顯示圓角和邊緣的那個。你想要的是在backgroundView之上的contentView。它涵蓋了細胞的通常白色區域。
這完美的作品對我來說:
NSEnumerator *enumerator = [cell.subviews objectEnumerator];
id anObject;
while (anObject = [enumerator nextObject]) {
if([anObject isKindOfClass: [ UIView class] ])
((UIView*)anObject).backgroundColor = [UIColor lightGrayColor];
}
我寫將在iPhone 3.0或更高的工作,但是使用白色背景,否則版本。
在你的UITableViewController
的viewDidLoad
方法中,我們添加以下內容:
self.view.backgroundColor=[UIColor clearColor];
// Also consider adding this line below:
//self.tableView.separatorColor=[UIColor clearColor];
當你創建你的細胞(在我的代碼,這是我的tableView:cellForRowAtIndexPath:
)添加以下代碼:
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"code_bg.png"]];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.0)
{
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
}
我知道這是一箇舊帖子,但我相信有些人仍在尋求幫助。您可以使用此設置一個individiual單元格的背景顏色,起初的工作原理:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
[cell setBackgroundColor:[UIColor lightGrayColor]];
然而,一旦你開始滾動,iPhone將重用的小區,這攪亂不同的背景顏色(如果你想交替他們)。你需要調用tableView:willDisplayCell:forRowAtIndexPath。這樣,在重新加載標識符之前設置背景色。你可以這樣做:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = ([indexPath row]%2)?[UIColor lightGrayColor]:[UIColor whiteColor];
}
最後一行只是一個濃縮的if/else語句。祝你好運!
您可以設置backgroundView
的backgroundColor
。如果backgroundView
不存在,您可以爲其創建一個。
if (!tableView.backgroundView) {
tableView.backgroundView = [[UIView alloc] initWithFrame:tableView.bounds];
}
tableView.backgroundView.backgroundColor = [UIColor theMostFancyColorInTheUniverse];
如果要設置到圖像的單元格的背景,然後使用此代碼:
// Assign our own background image for the cell
UIImage *background = [UIImage imageNamed:@"image.png"];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
- 1. 如何爲UITableView單元設置複合背景顏色?
- 2. UITableview背景顏色-iphone
- 3. iPhone Dev,UITableView背景顏色
- 4. 設置UITableViewCell和UITableView背景的顏色
- 5. 如何設置DataGridView中單個單元格的背景顏色?
- 6. 設置Fullcalendar單元格背景顏色
- 7. 如何在UITableView中設置UITableViewCell的選定背景顏色
- 8. UItableview單元格背景顏色
- 9. UITableView背景顏色設置兩次
- 10. 如何在asp.net中設置gridview單元背景顏色
- 11. 如何設置HandOnTable中每個單元格的背景顏色?
- 12. 如何設置單個gridview單元格的背景顏色
- 13. 在iPhone上設置表格視圖單元格的背景顏色
- 14. 如何設置單元格背景顏色在jqgrid中的單元格值baced?
- 15. 如何設置ap元素的背景顏色也爲上面的圖像設置背景顏色
- 16. 如何在iPhone中爲UIWebView設置背景顏色sdk
- 17. 如何使用VBA設置Excel單元格的背景顏色?
- 18. 如何使用iText設置表格單元的背景顏色?
- 19. 如果可能,如何設置Sizer中的單元格背景顏色或Sizer的網格線背景顏色?
- 20. 設置LI元素的背景顏色
- 21. 如何在jquery中設置圖片元素的背景顏色
- 22. 如何在menudrawer中設置活動元素的背景顏色
- 23. 在CodenameOne中設置日曆單元格的背景顏色
- 24. 在preferences.xml上設置背景顏色
- 25. 在WPF上設置背景顏色ContentView
- 26. 在listviewrow上設置背景顏色
- 27. 我如何設置iPhone中特定按鈕的背景顏色?
- 28. 如何在iPhone上的表格視圖中設置單個單元格的背景顏色
- 29. 如何設置NSTabView的背景顏色?
- 30. 如何設置menuitem的背景顏色
我已經加入此行中的cellForRowAtIndexPath:方法cell.backgroundView.backgroundColor = [的UIColor orangeColor] ;。它仍然顯示在白色背景上的黑色文字 – ebaccount 2009-05-22 18:34:50
我試過cell.backgroundColor = [UIColor orangeColor];這也不起作用 – ebaccount 2009-05-22 18:37:57