开发者

Change background Color of custom UITableViewCell once clicked not working well

I want to update the background color of a cell in my tableview once the user clicks it. The thing is if i disable the selection style by calling:

[code] [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [/code]

The color changes as i want but there is no visual feedback to the user of the selection before the color change, which is not desirable for my application.

When i remove that line and change the color like this only in an IBAction function:

[code] [cell setBackgroundColor:[UIColor lightGrayColor]]; [/code]

I end up with hav开发者_JAVA技巧ing a squared rectangle in the middle of the cell with the original colour before selection, and borders of the cell with the color i want to set. If i call my IBAction function twice the background color is set properly.

I am not sure whats the cause or how to resolve it.

Please bare in mind that i do all my cell setup (setting text, background color, text shadow, etc) in cellForRowAtIndexPath. I tried the other way of doing it in willDisplayCell but still with no luck.

Hope some one can spot some light on the matter :) Cheers AF


I managed to find a partial solution to my problem and here it is in case others stumble over the same issue:

// My cell disabling function (i use the cell as a button hence the postfix Btn)
-(void) disableLoginBtn
{
    loginBtnPtr.userInteractionEnabled = NO;
    [loginBtnPtr setBackgroundColor:[UIColor lightGrayColor]];
    [loginBtnPtr.textLabel setShadowColor: [UIColor whiteColor]]; 
}

-(void) someFunction
{
    ...
    // loginBtnPtr a pointer to the cell in my table i want to change its background color.
    // NOTE: If i include this setSelectionStyle function inside my disableBtn function the highlighting will be disabled all together and you wont get the effect you may be after.
    [loginBtnPtr setSelectionStyle:UITableViewCellSelectionStyleNone];
    [self disableLoginBtn];
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    ...
    [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    // Make sure you reset the selection color to the one you are using, in my case blue. Otherwise the highlighting will be disabled for that cell through out the application 
    [loginBtnPtr setSelectionStyle:UITableViewCellSelectionStyleBlue];
}

Hope it helps AF

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜