Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UILabel来自自定义单元格的阴影选定颜色_Iphone_Uitableview_Uilabel - Fatal编程技术网

Iphone UILabel来自自定义单元格的阴影选定颜色

Iphone UILabel来自自定义单元格的阴影选定颜色,iphone,uitableview,uilabel,Iphone,Uitableview,Uilabel,我正在加载一个自定义nib文件以自定义UITableView的单元格。自定义nib有一个UILabel,它通过标记从主视图中引用。我想知道,当单元格被选择为不同的颜色时,是否可以更改UILabel的阴影颜色,这样它就不会出现在屏幕截图中 您可以在代理的-tableView:willSelectRowAtIndexPath:中更改标签的阴影颜色。例如: -(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPat

我正在加载一个自定义nib文件以自定义UITableView的单元格。自定义nib有一个UILabel,它通过标记从主视图中引用。我想知道,当单元格被选择为不同的颜色时,是否可以更改UILabel的阴影颜色,这样它就不会出现在屏幕截图中


您可以在代理的
-tableView:willSelectRowAtIndexPath:
中更改标签的阴影颜色。例如:

-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.shadowColor = [UIColor greenColor];
    return indexPath;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.shadowColor = [UIColor redColor];
}

我更喜欢在TableCell代码中更改阴影颜色,以免污染代理。您可以重写此方法来处理它:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate
{
    UIColor * newShadow = highlighted ? [UIColor clearColor] : [UIColor whiteColor];

    nameLabel.shadowColor = newShadow;

    [super setHighlighted:highlighted animated:animate];
}

最简单的答案,至少对于上面所示的例子来说,是首先不显示阴影。由于您无法在白色上看到白色,请将阴影颜色设置为-clearColor


如果您确实需要阴影,那么重写-setHighlighted方法是最好的解决方案。它将代码保存在单元格中,我认为这比试图从表视图处理代码要好。

我也有同样的问题,上面的解决方案对我都不起作用-我不想子类化
UITableViewCell
,还以编程方式进行了一些棘手的选择/突出显示状态更改,这与上面的解决方案不起作用

我的解决方案:

我最后做的是在主
UILabel
下面使用第二个
UILabel
作为阴影。对于该“阴影”
UILabel
,您可以将“突出显示的颜色”设置为“清晰颜色”

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


希望有帮助

此答案不适用于高亮显示的单元格状态(即,如果用户按下单元格但不释放)。不幸的是,杰森的回答是我见过的处理这个案子的最好答案。不幸的是,它涉及子类化。您可能还需要覆盖以下选项: