Ios UiTableViewCell中的视图在选择时变为透明
我有一个自定义的Ios UiTableViewCell中的视图在选择时变为透明,ios,uitableview,custom-controls,selection,Ios,Uitableview,Custom Controls,Selection,我有一个自定义的UITableViewCell,它带有一个红色的UIView,当我选择UITableViewCell时,它会变成透明的 我不希望这种情况发生。有没有办法在选择时保持ui视图的颜色 只有与该视图相关的配置 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCellIdentifie
UITableViewCell
,它带有一个红色的UIView
,当我选择UITableViewCell
时,它会变成透明的
我不希望这种情况发生。有没有办法在选择时保持ui视图的颜色
只有与该视图相关的配置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellIdentifier = @"Cell Identifier";
DetailsCell *cell = (DetailsCell *) [tableView dequeueReusableCellWithIdentifier: kCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.myView.layer.cornerRadius = 5;
cell.myView.layer.masksToBounds = YES;
return cell;
}
将标签添加到视图中
yourview.tag = 3;
然后在tableView的didselectRowatinexPath
方法中添加此代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get the cell which is selected
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
//set tempView color of selected cell bcoz when cell selected all view color is gone
UIView *tempView=[selectedCell viewWithTag:3];
//set your color whatever you want
tempView.backgroundColor = [UIColor colorWithRed:(112/255.0) green:(136/255.0) blue:(213/255.0) alpha:1];
}
更新
也可以通过将单元格选择颜色选择为“无”来完成此操作。将此代码放入cellforrowatinexpath
cell.selectionStyle = UITableViewCellSelectionStyleNone;
可以使用所需颜色设置自定义背景视图。您执行此操作的位置将因您配置单元的方式而异
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = // Your color (cell.backgroundColor perhaps);
cell.selectedBackgroundView = bgColorView;
如果您确实希望显示所选内容(使用蓝色),但将视图保留为您提供的颜色,则可以将selectionStyle设置为零,就像您在回答中所做的那样(或在IB中设置),然后在代码中执行此操作:
在RDCell.m中:
@实现RDCell
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = [UIColor clearColor];
}
return self;
}
在“表视图”控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.label1.text = self.theData[indexPath.row];
if ([self.selectedPath isEqual:indexPath]) {
cell.backgroundView.backgroundColor = [UIColor blueColor];
}else{
cell.backgroundView.backgroundColor = [UIColor clearColor];
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedPath = indexPath;
[self.tableView reloadData];
}
您还可以使用UITableViewCell的setHighlighted方法撤消默认行为所做的任何更改。您可以添加您的代码吗?代码添加到问题中几乎就是这样。问题是,当我选择UIView时,它仍然是透明的,只有当我取消选择时,它才会变成所选的颜色。我不想更改UITableViewCell所选的颜色。这是我在UITableViewCell.Solution中的视图-在单元格中:override func setHighlighted(uhighlighted:Bool,animated:Bool){super.setHighlighted(highlighted,animated:animated)imageView.backgroundColor==UIColor.white}