簡體   English   中英

在iPhone上設置表格視圖單元格的背景顏色

[英]Setting background color of a table view cell on iPhone

我想達到這樣的效果,其中表視圖的一個單元格將具有藍色背景,下一個將具有白色,下一個將再次具有藍色,然后是白色等等...您能告訴我我該怎么辦?去做?

謝謝。

將此方法添加到表視圖委托:

#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView 
  willDisplayCell: (UITableViewCell*)cell 
forRowAtIndexPath: (NSIndexPath*)indexPath
{
    cell.backgroundColor = indexPath.row % 2 
        ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
        : [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}

您必須設置單元格內容視圖的背景顏色

cell.contentView.backgroundColor = [UIColor colorWithRed...]

這將設置整個單元格的背景。

要對備用單元格執行此操作,請使用indexPath.row% by 2。

如果要根據實際單元格數據對象中的某些狀態設置單元格顏色,則這是另一種方法:

如果將此方法添加到表視圖委托:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}

然后在您的cellForRowAtIndexPath方法中,您可以執行以下操作:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

這樣可以節省在willDisplayCell方法中再次檢索數據對象的麻煩。

請在cellForRowAtIndexPath中添加以下代碼

if (indexPath.row % 2 == 0){
    cell.backgroundColor =[UIColor blueColor];
} else {
    cell.backgroundColor =[UIColor whiteColor];
}

我想這會對你有所幫助

只要您不使用UITableViewCell的內置標簽,lostInTransit的答案中的cell.contentView.backgroundColor = [UIColor colorWithRed...]方法就可以正常工作。

我發現如果您使用內置標簽,例如通過設置cell.text ,您最終會在標簽cell.text出現一個不透明的白色塊,並且只有單元格的兩端顯示您想要的顏色。

我發現無法編輯內置標簽,因此它是非透明的(您可以通過UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]訪問它)。

我通過添加自己的自定義UILabel解決了這個問題。 像這樣:

UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
cellLabel.text = @"Test with non-opaque label";
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.opaque = NO;

[cell.contentView addSubview:cellLabel];
cell.contentView.backgroundColor = [UIColor darkGrayColor];

//節省時間的方法:

//在索引方法的單元格中:

static NSString* evenIdentifier = @"even";
static NSString* oddIdentifier = @"odd";

__weak identifier;
if(indexPath.row %2 ==0)
{
    identifier = evenIdentifier;
}else{
    identifier = oddIdentifier;
}

cell = dequeue..WithIdentifier: identifier;
if(cell == nil){
    cell = allocOrLoadNib...;
    cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor);
}

//更改單元格內容然后返回它。 輕松工作。

//這是大綱代碼。 請不要直接復制。

這對我有用..在cellForRowAtIndexPath中,

cell.textLabel.backgroundColor = [UIColor clear];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want
cell.backgroundColor = cell.contentView.backgroundColor;

對於您的要求,如前面提到的基於indexPath%2的值,您可以執行上述功能。

使用默認表格單元格設置時,以下兩個屬性將為單元格的背景和標簽的背景顏色着色,從而無需創建自定義標簽作為單元格contentView的子視圖。

cell.textLabel.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor redColor];

對於使用內置文本標簽並且不希望文本標簽的白色背景顏色模糊背景顏色的情況,此代碼是一種稍微更干凈的解決方案。 這也解決了違反表視圖分組樣式的圓角的問題(當您使用cell.contentView.backgroundColor而不是cell.backgroundColor時會發生這種情況):

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
    cell.backgroundView = bg;
    [bg release];

您需要做的就是在表的視圖控制器實現文件中添加以下代碼。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
    UIColor *altCellColor = [UIColor blackColor];
    cell.backgroundColor = altCellColor;
}}

然后就像添加和更改模數值一樣簡單。

backgroundView添加到單元格並設置其選擇的背景顏色。

let backgroundView = View() //  add background view via code or via storyboard
view.backgroundColor = UIColor.red // your color
cell.backgroundView = backgroundView

暫無
暫無

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

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