簡體   English   中英

iOS / iPhone-設置任意行的顏色

[英]iOS / iPhone - setting the color of an arbitrary row

我正在嘗試使用以下代碼在UITableView中設置所選行的顏色:

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

問題是,它正在所有行上設置顏色。 此外,當我選擇特定行時,它會以藍色突出顯示該行。 如何獲得以紅色突出顯示所選行的信息?

對我來說,最簡單的方法是將UITableViewCell子類化並覆蓋setSelected:方法(如果需要,其他所有內容都保留相同)

一些來自舊項目的示例代碼:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    UIView *bg = [[UIView alloc] initWithFrame:self.frame];
    bg.backgroundColor = [UIColor colorWithRed:.916 green:.9648 blue:.9844 alpha:1.0];
    self.selectedBackgroundView = bg;
    [bg release];
    // Configure the view for the selected state
}

暫無
暫無

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

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