iphone – UITableViewCell在取消选择时选择了阴影颜色

iphone – UITableViewCell在取消选择时选择了阴影颜色,第1张

概述我有一个标准的UITableView.我想将单元格textLabel的shadowColor设置为[UIColor whiteColor],但仅限于触摸单元格时.为此,我使用以下代码.它是一个自定义的UITableViewCell子类,它覆盖setSelected / setHighlighted: @implementation ExampleTableViewCell- (void)se @H_419_6@ 我有一个标准的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在取消选择时选择了阴影颜色所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1080758.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存