ios - 选择时 View 从 UITableViewCell 中消失

标签 ios uiview uitableview

我有一个带有自定义 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/

相关文章:

ios - 如何从 Photoshop 获取正确的字体大小到 iOS

iphone - 如何在 xcode 4.2 中创建基于菜单的 ios 应用程序

objective-c - 重新加载 TableView 内容时出现 EXC_BAD_ACCES 错误

ios - 如何在 Web 服务 swift 中以 POST 形式发送图像

ios - iOS 上的 Instagram Oauth 重定向

ios - 如何使用 ARC 释放内存

ios - TableView Cell Tapped 在 Segue 中返回 Nil

ios - 删除对象作为某些 KVO 属性的观察者的最佳实践

ios - UIKit Dynamics 和 CALayers 与 UIView 分离

iphone - 为什么从 Nib 加载的 View 被放置在状态栏下方 20 像素处?