簡體   English   中英

如何更改tableViewCell背景色

[英]How to change tableViewCell background color

注意:這是關於GROUPED tableView中單元格 與普通的tableView相比,這有很大的不同! 默認的單元格自定義答案在這種情況下不起作用,因此請先驗證您的答案。

這是我設置灰色屏幕黃色tableView背景的方式:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    UIView *myView = [[UIView alloc] initWithFrame:magicRect];
    myView.backgroundColor = [UIColor yellowColor];
    [self.tableView addSubview:myView];
    [self.tableView sendSubviewToBack:myView];
}

這就是我設置綠色單元格背景的方式。 從圖片中可以看到,它缺少一些區域:

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell =
   [tableView dequeueReusableCellWithIdentifier:@"Cell"];
 if (cell == nil) {
   cell = [[[UITableViewCell alloc]
     initWithStyle:UITableViewCellStyleValue1
     reuseIdentifier:@"Cell"] autorelease];
   cell.backgroundColor = [UIColor greenColor];
 }
 // Configure the cell...
}

問題:如何在tableView單元格的開頭和結尾更改顏色 現在,該單元格在這些區域中是透明的,並在整個tableView下方顯示self.view.backgroundColor。 這些區域實際上是透明的,因為在滾動tableView時,帶紋理的背景保持在同一位置。

在此處輸入圖片說明

像這樣將tableView背景設置為清晰的顏色,

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     [tableView setBackgroundColor:[UIColor clearColor]];//Here you can give as yellow instead of adding view
     //ur code
}

我不知道為什么要向表視圖添加子視圖,可以為表視圖設置背景顏色:

tview.backgroundColor=[UIColor yellowColor];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM