avf*_*avf 5 iphone objective-c uitableview ipad ios
我有一个标准的UITableView.我想shadowColor将单元格设置textLabel为[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
Run Code Online (Sandbox Code Playgroud)
我对这种方法的问题是,在取消选择时,单元格的标签文本和阴影都是白色的周期非常短.请参阅此屏幕截图,该截图是在取消选择的确切时刻拍摄的:

它与这两个帖子的方法基本相同:
我在后一个问题中使用了接受答案的方法.
我创建了一个非常简单的代码项目并将其上传到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!
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
如果我理解了这个问题,你需要显示阴影颜色,直到细胞选择被动画化为止.我不确定你尝试的方式有什么不对,更直接的解决方案可以正常工作.
请注意,一旦不需要,您将需要删除观察者.
ExampleTableViewCell.h
@interface ExampleTableViewCell : UITableViewCell {
}
- (void) setSelectionShadowOfColor:(UIColor *) selColor;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2332 次 |
| 最近记录: |