簡體   English   中英

選定的行背景顏色在iOS中未更改

[英]selected row background color not changing in ios

我不知道為什么選定的行顏色在tableview中沒有改變的原因。

我創建了自定義單元格並將漸變顏色應用於單元格的背景,並且我編寫了使用所選背景來更改bgcolor的代碼。 問題是,如果我不使用漸變色,效果很好,但是如果使用漸變色,則不會更改bgcolor。

確切的問題是什么? 任何解決方案?

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

    static NSString *cellidentifier=@"ViewProfileCell";

    MyHomeViewCell *cell= [[MyHomeViewCell alloc] init];

    cell=(MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if(!cell)
    {
NSArray *nibofMyHomeCell=[[NSBundle mainBundle]loadNibNamed:@"MyHomeViewCell" owner:self options:Nil];
        cell=[nibofMyHomeCell objectAtIndex:0];


    }
    UIView *v=[[UIView alloc]init];

    v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor];

    cell.selectedBackgroundView=v;

   CAGradientLayer *ViewGradient=[CAGradientLayer layer];
    ViewGradient.frame=CGRectMake(0, 0, 320, 52);
    ViewGradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:27.0f / 255.0f green:48.0f / 255.0f blue:39.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:26.0f / 255.0f green:47.0f / 255.0f blue:38.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:25.0f / 255.0f green:44.0f / 255.0f blue:37.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:23.0f / 255.0f green:42.0f / 255.0f blue:35.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:41.0f / 255.0f blue:34.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:40.0f / 255.0f blue:33.0f / 255.0f alpha:1.0f] CGColor],
                           nil];

    [cell.layer insertSublayer:ViewGradient atIndex:0];
    cell.layer.shadowColor = [[UIColor whiteColor] CGColor];
    cell.layer.shadowOpacity=20.0f;

    cell.layer.borderWidth=1.0f;

    cell.layer.borderColor=[[UIColor colorWithRed:38.0f / 255.0f green:70.0f / 255.0f blue:58.0f / 255.0f alpha:1.0f] CGColor];

}

嘗試避免在-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中使用此代碼

    UIView *v=[[UIView alloc]init];

    v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor];

    cell.selectedBackgroundView=v;

   CAGradientLayer *ViewGradient=[CAGradientLayer layer];
    ViewGradient.frame=CGRectMake(0, 0, 320, 52);
    ViewGradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:27.0f / 255.0f green:48.0f / 255.0f blue:39.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:26.0f / 255.0f green:47.0f / 255.0f blue:38.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:25.0f / 255.0f green:44.0f / 255.0f blue:37.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:23.0f / 255.0f green:42.0f / 255.0f blue:35.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:41.0f / 255.0f blue:34.0f / 255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:22.0f / 255.0f green:40.0f / 255.0f blue:33.0f / 255.0f alpha:1.0f] CGColor],
                           nil];

    [cell.layer insertSublayer:ViewGradient atIndex:0];
    cell.layer.shadowColor = [[UIColor whiteColor] CGColor];
    cell.layer.shadowOpacity=20.0f;

    cell.layer.borderWidth=1.0f;

    cell.layer.borderColor=[[UIColor colorWithRed:38.0f / 255.0f green:70.0f / 255.0f blue:58.0f / 255.0f alpha:1.0f] CGColor];

每次調用此方法時,都會創建UIView v並將其應用於您的單元格,因此在滾動tableview時會導致巨大的內存開銷。

至於這個問題,因為您是UITableViewCell的子類,請在-(void)drawRect:(CGRect)rect或init中實現常規背景的代碼。 然后創建UIView,它將作為您選定背景的漸變並應用於您的單元格子類:

- (id)init {
    self = [super init];
    if (self) {
        self.contentView.backgroundColor = [UIColor anyColourYouNeed];
        UIView *v=[[UIView alloc]init];

        v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor];

        cell.selectedBackgroundView=v;
    }
    return self;
}

希望這對您有所幫助,加油!

暫無
暫無

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

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