【发布时间】:2014-01-10 08:30:59
【问题描述】:
我有一个带有自定义 UITableViewCells 的 UITableView。单元格的内容保存在扩展 UIView 的单独类中(我有 3 种内容,我通过隐藏和显示视图进行切换,我采用这种方法是因为我想在单元格之间进行转换)。 所以让我们假设我有一个可见的视图并添加到单元格的 contentView 中。这个视图包含一些文本和一些由 UIViews 组成的矩形。到目前为止一切都很好,但奇怪的是当我触摸单元格时,矩形消失了,文本保持不变。你知道可能是什么问题吗?我也尝试将视图添加到 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
【问题讨论】:
-
您尚未将其添加到内容视图中。您已将其添加到背景视图中,当您选择单元格时它不会显示。
-
嗨,如果你想尝试,我添加了来源,我在哪里添加我的视图并不重要,因为一切都是透明的。
-
透明无关紧要。当您选择单元格时,backgroundView 会隐藏,不会发送到后面,因此您添加到其中的任何内容都会消失。将您的视图添加到内容视图。
-
这就是我的意思,不是所有的东西都消失了,文字也没有消失。然而,根据Apple关于selectedBackgroundView的说法,backgroundView永远不会消失:“UITableViewCell仅在选择单元格时将此属性的值添加为子视图。如果不是,它将所选背景视图添加为直接在背景视图(backgroundView)上方的子视图nil,或在所有其他视图后面。调用 setSelected:animated: 会导致选定的背景视图以 alpha 淡入淡出动画。"
-
你说得对,对不起。不过,我仍然认为您应该将其添加到内容视图中。
标签: ios uiview uitableview