我有一个自定义的UITableViewCellSelectionStyleBlue风格的UITableViewCell。它里面有一个UITextField。
每当单元格被突出显示/选中(即背景为蓝色)时,我希望UITextField具有白色,就像内置标签自动执行的那样。
当然,这包括确保文本字段的颜色是正确的,同时背景色的“白转蓝”或“蓝转白”淡出动画正在发生。
我该如何正确地完成这项工作?
发布于 2012-02-03 03:35:43
我认为最好的方法是创建表格视图单元格的子类,并覆盖setSelected:animated:
在此方法中,您将根据选定的标志将字段的文本颜色更改为所需颜色
要设置动画,只需将其放入动画块中:
[UIView animateWithDuration:.3f animations:^{
field.textColor = [UIColor whiteColor];
}];发布于 2012-02-04 02:46:42
因为这个单元格是“自定义”的,所以我假设你已经创建了一个子类。
首先,UITableViewCellSelectionStyleBlue不是UITableViewCellStyle,它是一种选择样式,因此不应该在-init方法中使用它,但可以使用它,因为它的值将与UITableViewCellStyle相同。
此外,为了在选择时编辑单元格,您需要覆盖-setSelected:animated:方法。此外,您需要在选择时将单元格的textField的textColor变量设置为白色,反之亦然。
- (void) setSelected:(BOOL) selected animated:(BOOL) animated {
self.textField.textColor = (selected == YES ? [UIColor whiteColor] : [UIColor blueColor]);
[super setSelected:selected animated:animated];
}否则,最好在-tableView:didSelectRowAtIndexPath:方法中调用一个方法。
- (void) tableView:(UITableView *) _tableView didSelectRowAtIndexPath:(NSIndexPath *) _indexPath {
[self rowSelected:_indexPath];
}
- (void) rowSelected:(NSIndexPath *) indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
BOOL isSelected = cell.isSelected;
[(UITextField *) [cell viewWithTag:kTextFieldTag] setTextColor:(isSelected == YES ? [UIColor whiteColor] : [UIColor blueColor])];
}发布于 2012-02-09 23:15:14
如果您是在界面生成器中设计自定义单元格的,则UITextField在属性检查器中有一个默认为黑色的“突出显示”属性。将其设置为白色,文本颜色会在按下时发生变化。
https://stackoverflow.com/questions/9100683
复制相似问题