iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – UITableViewCell在取消选择时选择了阴影颜色大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个标准的UITableView.我想将单元格textLabel的shadowColor设置为[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

我对这种方法的问题是,在取消选择时,单元格的标签文本和阴影都是白色的周期非常短.请参阅此屏幕截图,该截图是在取消选择的确切时刻拍摄的:

它与这两个帖子的方法基本相同:

UILabel shadow from custom cell selected color

Removing text shadow in UITableViewCell when it’s selected

我在后一个问题中使用了接受答案的方法.

我创建了一个非常简单的代码项目和uploaded it to github.它显示了我的问题.它只是一个显示单个单元格的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!
}

有任何想法吗?

解决方法

如果我理解了这个问题,你需要显示阴影颜色,直到细胞选择被动画化为止.我不确定你尝试的方式有什么不对,更直接的解决方案可以正常工作.

请注意,一旦不需要,您将需要删除观察者.

ExampleTableViewCell.h

@interface ExampleTableViewCell : UITableViewCell {

}

- (void) setSELEctionShadowOfColor:(UIColor *) selColor;
@end

ExampleTableViewCell.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

ExampleTableViewController.m

- (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;
}

大佬总结

以上是大佬教程为你收集整理的iphone – UITableViewCell在取消选择时选择了阴影颜色全部内容,希望文章能够帮你解决iphone – UITableViewCell在取消选择时选择了阴影颜色所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。