我有一个带有自定义 UITableViewCells 的 UITableView。单元格的内容保存在扩展 UIView 的单独类中(我有 3 种内容,我通过隐藏和显示 View 来切换,我采用这种方法是因为我想在单元格之间进行转换)。 因此,假设我有一个 View 可见并添加到单元格的 contentView 中。这个 View 包含一些文本和一些由 UIView 组成的矩形。到目前为止一切都很好,但奇怪的是当我触摸单元格时,矩形就消失了,文本保持不变。你知道可能是什么问题吗?我也尝试将 View 添加到 backgroundView,它在做同样的事情。
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.active = NO;
state = -1;
// Touch is not working if the view has no bounds
self.backgroundView = [[UIView alloc] init];
self.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = [[UIView alloc] init];
self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
self.contentView.clipsToBounds = YES;
// Add empty view
emptyView = [[View1 alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) andRadius:CELL_DOT_RADIUS];
emptyView.userInteractionEnabled = NO;
[self.backgroundView addSubview:emptyView];
观点:
- (id) initWithFrame:(CGRect)frame andRadius:(int)r {
self = [super initWithFrame:frame]; radius = r;
if (self) {
int outerlineWeight = 1;
timelineView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/4, frame.size.height/2, 1, 1)];
[self addSubview:timelineView];
dotView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/4-radius, frame.size.height/2-radius, radius*2, radius*2)];
dotView.layer.cornerRadius = radius;
dotView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self addSubview:dotView];
UIView *n = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 50, 20)];
n.backgroundColor = [UIColor redColor];
[self addSubview:n];
middleText = [[UILabel alloc] initWithFrame:dotView.frame];
middleText.font = [UIFont systemFontOfSize:12];
middleText.textColor = [UIColor grayColor];
middleText.backgroundColor = [UIColor clearColor];
middleText.textAlignment = NSTextAlignmentCenter;
[self addSubview:middleText];
这些是资源,因此您可以根据需要自己尝试。如您所见,橙色矩形消失了,但文本没有消失。对于我需要做的事情,什么都不应该消失,在最好的情况下,我想改变它们的颜色。 http://ge.tt/6wRBObD1/v/0?c
最佳答案
您正在将它添加到背景 View 中。选择后,所选背景 View 将显示为非背景 View 。在您的自定义单元格中尝试
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
[self.backgroundView addSubview:emptyView];
}
else{
[self.selectedBackgroundView addSubview:emptyView];
}
// Configure the view for the selected state
}
或者添加空 View 到contentview
关于ios - 选择时 View 从 UITableViewCell 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039366/