swi*_*ode 46 colors objective-c uitableview ios
我有这样的菜单:
每个单元格的正常(未选定)状态是图像,所选状态也是图像(看起来像默认的蓝色图像).但是,我想添加一个额外的第三个图像,这样当用户选择一个单元格时,它会在变为蓝色(选中)之前短暂更改为第三个颜色.
这是我的代码:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setBackgroundColor:[UIColor clearColor]];
NSString *cellIdentifier = @"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
UIImage *cellBackgroundNormal = [UIImage imageNamed:@"cell_menu_normal"];
UIImage *cellBackgroundSelected = [UIImage imageNamed:@"cell_menu_selected"];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:cellBackgroundNormal];
UIImageView *cellBackgroundSelectedView = [[UIImageView alloc] initWithImage:cellBackgroundSelected];
cell.backgroundView = cellBackgroundView;
cell.selectedBackgroundView = cellBackgroundSelectedView;
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我现在只有两个州.我不知道如何cell.hoveredBackgroundView
为第三张图片介绍某种形式.如果有人能帮我实现这一点,我真的很感激.
Ayu*_*ush 93
iOS 6.0及更高版本
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Add your Colour.
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[self setCellColor:[UIColor whiteColor] ForCell:cell]; //highlight colour
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Reset Colour.
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color
}
- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
cell.contentView.backgroundColor = color;
cell.backgroundColor = color;
}
Run Code Online (Sandbox Code Playgroud)
自定义UITableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
UIView * selectedBackgroundView = [[UIView alloc] init];
[selectedBackgroundView setBackgroundColor:[UIColor colorFromHexString:@"5E6073"]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];
}
Run Code Online (Sandbox Code Playgroud)
AG1*_*AG1 32
iOS 8.0(及更高版本)使用Swift
斯威夫特2
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.contentView.backgroundColor = UIColor.orangeColor()
cell?.backgroundColor = UIColor.orangeColor()
}
override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.contentView.backgroundColor = UIColor.blackColor()
cell?.backgroundColor = UIColor.blackColor()
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.contentView.backgroundColor = UIColor.orange
cell?.backgroundColor = UIColor.orange
}
override func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.contentView.backgroundColor = UIColor.black
cell?.backgroundColor = UIColor.black
}
Run Code Online (Sandbox Code Playgroud)
Mil*_*mak 32
你也可以像这样使用UIAppearance:
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor redColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
Run Code Online (Sandbox Code Playgroud)
这将适用于UITableViewCell的所有实例或您可能具有的任何子类.只需确保selectionStyle
单元格的属性未 设置为UITableViewCellSelectionStyleNone
.
mar*_*ing 14
比接受的答案更容易:
在您的UITableViewCell子类中:
在awakeFromNib
或init
:
self.selectionStyle = UITableViewCellSelectionStyleNone;
然后:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
self.backgroundColor = [UIColor yourHighlightColor];
}
else {
self.backgroundColor = [UIColor yourNormalColor];
}
}
Run Code Online (Sandbox Code Playgroud)
在定制单元格中尝试此操作
- (void)awakeFromNib
{
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
selectedBackgroundView.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:95.0/255.0 blue:22.0/255.0 alpha:1.0];
self.selectedBackgroundView = selectedBackgroundView;
}
Run Code Online (Sandbox Code Playgroud)
迅速:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let selectionColor = UIView() as UIView
selectionColor.layer.borderWidth = 1
selectionColor.layer.borderColor = UIColor.blueColor().CGColor
selectionColor.backgroundColor = UIColor.blueColor()
cell.selectedBackgroundView = selectionColor
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
let selectionColor = UIView() as UIView
selectionColor.layer.borderWidth = 1
selectionColor.layer.borderColor = UIColor.blue.cgColor
selectionColor.backgroundColor = UIColor.blue
cell.selectedBackgroundView = selectionColor
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
90240 次 |
最近记录: |