Iphone UITableViewCell取消选择时选定的阴影颜色

Iphone UITableViewCell取消选择时选定的阴影颜色,iphone,objective-c,ios,ipad,uitableview,Iphone,Objective C,Ios,Ipad,Uitableview,我有一个标准的UITableView。我想将单元格的文本标签的阴影颜色设置为[UIColor whiteColor],但仅当触摸单元格时。为此,我使用以下代码。它是一个自定义UITableViewCell子类,覆盖setSelected/setHighlighted: @implementation ExampleTableViewCell - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setS

我有一个标准的UITableView。我想将单元格的
文本标签的
阴影颜色设置为
[UIColor whiteColor]
,但仅当触摸单元格时。为此,我使用以下代码。它是一个自定义UITableViewCell子类,覆盖setSelected/setHighlighted:

@implementation ExampleTableViewCell


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

}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    [self setShadowColorSelected:highlighted];
}

- (void)setShadowColorSelected:(BOOL)selected {
    if (selected) {
        self.textLabel.shadowColor = [UIColor blackColor];
    }else {
        self.textLabel.shadowColor = [UIColor whiteColor];
    }
}

@end
我对这种方法的问题是,在取消选择时,单元格有一个非常短的周期,标签的文本和阴影都是白色。请参见此屏幕截图,该截图是在取消选择时拍摄的:

这与这两篇文章中的方法基本相同:

在后一个问题中,我使用的是公认答案的方法

我创建了一个非常简单的代码项目和。它显示了我的问题。它只是一个显示单个单元格的UITableViewController

除此之外,没有什么新奇的东西。UITableView委托方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[ExampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"test";

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; //setting this to NO doesn't work either!
}

有什么想法吗?

这是我能做的最好的了:

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

    [self setShadowColorSelected:selected];
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:YES];

    [self setShadowColorSelected:highlighted];
}

- (void)setShadowColorSelected:(BOOL)selected {
    [self.textLabel setBackgroundColor:[UIColor clearColor]];

    if (selected)
    {
        [self.textLabel setTextColor:[UIColor whiteColor]];
        [self.textLabel setShadowColor:[UIColor blackColor]];

        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView transitionWithView:self.textLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            if (selected)
            {
                [self.textLabel setTextColor:[UIColor whiteColor]];
                [self.textLabel setShadowColor:[UIColor blackColor]];
            }
            else
            {
                [self.textLabel setTextColor:[UIColor blackColor]];
                [self.textLabel setShadowColor:[UIColor whiteColor]];
            }
        } completion:nil];
    }
    else
    {
         [self.textLabel setTextColor:[UIColor blackColor]];
         [self.textLabel setShadowColor:[UIColor whiteColor]];
    }
}

根据我的理解,您可以通过使用以下代码和所需的颜色来更改tableview的颜色来消除此问题

[tableView setBackgroundColor:[UIColor "GrayColor"]];

以下是一个简单的方法来实现您的目标:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    self.textLabel.shadowColor = [UIColor blackColor];
    [super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    self.textLabel.shadowColor = [UIColor whiteColor];
    [super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    self.textLabel.shadowColor = [UIColor whiteColor];
    [super touchesCancelled:touches withEvent:event];
}

我想您应该需要以UITableView用于设置选择/取消选择动画的相同持续时间设置文本/阴影颜色变化的动画。据我所知,在tableView开始为选择高光的(dis)显示设置动画的那一刻,您更改了文本/阴影颜色,因此您得到的是您的颜色会立即更改,而选择高光需要一些时间来从一种状态设置动画到另一种状态

试着这样做:

__block UIColor *newShadowColor = selected ? [UIColor blackColor] : [UIColor whiteColor];
[UIView animateWithDuration:0.2
                 animations:^{
                        /* change your label/shadow color here. */
                        self.textLabel.shadowColor = newShadowColor;
                 }
                 completion:^(BOOL finished){
                       /* this is where the cell is no longer selected
                          or highlighted. You may do some additional style changes to your
                          label here */ }];

如果我理解了这个问题,您需要显示阴影颜色,直到单元格选择动画消失。我不确定您尝试的方式中有什么不正确,但更简单的解决方案很好

请注意,一旦不需要,您需要删除observer

示例TableViewCell.h

@interface ExampleTableViewCell : UITableViewCell {

}

- (void) setSelectionShadowOfColor:(UIColor *) selColor;
@end
示例TableViewCell.m

@implementation ExampleTableViewCell

- (void) setSelectionShadowOfColor:(UIColor *) selColor {
    self.textLabel
    [self addObserver:self
           forKeyPath:@"textLabel.highlighted" // not isHighlighted as that is a getter name of the highlighted property
              options:NSKeyValueObservingOptionNew
              context:NULL];
}


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    BOOL isHighlighted = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];

    if (isHighlighted) {
        self.textLabel.shadowColor = [UIColor blackColor];
    } else {
        self.textLabel.shadowColor = [UIColor whiteColor];
    }
}

@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ExampleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // note the type ExampleTableViewCell is used here to avoid the interface lookup mess
    if (!cell) {
        cell = [[ExampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionShadowOfColor:[UIColor blackColor]];
    }
    cell.textLabel.text = @"test";

    return cell;
}
示例TableViewController.m

@implementation ExampleTableViewCell

- (void) setSelectionShadowOfColor:(UIColor *) selColor {
    self.textLabel
    [self addObserver:self
           forKeyPath:@"textLabel.highlighted" // not isHighlighted as that is a getter name of the highlighted property
              options:NSKeyValueObservingOptionNew
              context:NULL];
}


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    BOOL isHighlighted = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];

    if (isHighlighted) {
        self.textLabel.shadowColor = [UIColor blackColor];
    } else {
        self.textLabel.shadowColor = [UIColor whiteColor];
    }
}

@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ExampleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // note the type ExampleTableViewCell is used here to avoid the interface lookup mess
    if (!cell) {
        cell = [[ExampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionShadowOfColor:[UIColor blackColor]];
    }
    cell.textLabel.text = @"test";

    return cell;
}

我也有同样的问题。我看到的所有解决方案都需要子访问/附加代码太多

我最后做的是在主
UILabel
下创建第二个
UILabel
作为阴影

不要在主标签和阴影标签上设置阴影。对于阴影标签,将“正常颜色”设置为您想要的阴影颜色,并将高亮显示的颜色设置为“清晰颜色”

显然,每次更新主标签时都必须更新阴影标签。在很多情况下,这不是一个很大的代价


希望有帮助

将以下内容添加到UITableViewDelegate:

NSIndexPath *selectedIndex;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[ExampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"test";
    if(indexpath == selectedIndex)
    {
        cell.textlabel.shadowColor = [UIColor blackColor];
    }
    else
    {
        cell.textlabel.shadowColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView beginUpdates];
    selectedIndex = indexpath;
    [self.tableView endUpdates];
}

我也面临着同样的问题

您可以使用UIButton,而不是使用默认标签,您的问题将得到解决

将自定义按钮放在单元格中


我的要求得到了解决。这可能会对您有所帮助。

我只是想大声说出来,但是您是否尝试过在
setSelected
setHighlighted
(其中一个)中设置一个条件,以便
setShadowColorSelected
仅在
BOOL
值为
true
时调用
?有可能两个方法中的一个在另一个之前被调用,这就是为什么阴影在高光被移除之前变为白色。我尝试了类似的方法,只实现了其中一个方法,所以要么
setSelected
要么
setHighlighted
。结果是同样的问题。我不知道这里的问题是什么,这种行为对我来说似乎是正确的。您不希望
setHighlighted:
setSelected:
在动画完成之前阻止线程,是吗?不,我不希望。问题是,在上面的屏幕截图中,
shadowColor
是白色的(这意味着在单元格上调用setSelected/setHighlighted时没有显示),但是
UILabel的
textColor
也是白色的,这是它的
highlightedTextColor
。因此标签仍处于“突出显示”状态,但单元格不是。既然您知道正在使用
NO
调用您的方法,那么您可以尝试在
setShadowColorSelected
方法中将
self.textLabel.textColor
设置为白色/黑色?谢谢,我尝试过了,但不幸的是,这并不能解决问题。此外,仅仅覆盖事件处理代码并不十分优雅,在某些情况下可能会导致问题。使用这种方法,您将在何处/以何种方法将阴影设置回白色?有了你发布的代码,即使取消选择,它也会保持黑色。我把颜色和你想要的颜色颠倒过来了。现在就修好了。如果你尝试一下代码,你会发现它是有效的。当调用touchesEnded或touchesCancelled时,阴影颜色将设置回原始颜色。只是尝试了一下,不起作用。给出了完全相同的结果,我可以用您的代码生成与上面相同的屏幕截图:(仔细看,当您放开单元格时,它只有一帧。谢谢,您的答案可以工作,但是在非动画取消选择时它看起来不太好(当调用
[self.tableView descrowatindexpath:indexath-animated:NO];
)。一定有更好的方法来解决这个问题,这是一件很常见的事情。抱歉,但这应该如何工作?
cellforrowatinexpath
甚至在选择时都没有调用。抱歉,我忘记更新
UITableView
,检查编辑后的答案,它现在应该可以工作了:)好的,是的,这可能真的可以工作,但是我不想在每次选择时都更新整个tableView。就像Rick Pastoor的另一个答案一样,在非动画取消选择时不起作用。好吧,只要将animationDuration设置为0,如果animated==NO in-setSelected:animated:如果我将animationDuration设置为0,这会如何