在自定义UIView的背景上查看tableViewLines

时间:2010-01-23 14:05:17

标签: iphone uiview

我有一个viewController,它有一个tableView和一个自定义加载UIView,在加载tableView的数据时显示加载消息和微调器。 自定义UIView有一个我可以看到的红色背景,但我仍然可以看到tableView的行,如何在不看背景的tableView行的情况下显示自定义uiview。

@implementation LoadingView

@synthesize spinner;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
        [self setUserInteractionEnabled:NO];
        [self setBackgroundColor:[UIColor blueColor]];

        // Spinner
        spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [spinner setCenter:CGPointMake(320/2, 150)];
        [self addSubview:spinner];
        [spinner startAnimating];

        //title label
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"Loading...";
        titleLabel.font = [UIFont boldSystemFontOfSize:18];
        titleLabel.lineBreakMode =  UILineBreakModeWordWrap;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = UITextAlignmentLeft;
        CGFloat height = [titleLabel.text sizeWithFont: titleLabel.font constrainedToSize: CGSizeMake(300, 1500) lineBreakMode: UILineBreakModeWordWrap].height;
        [titleLabel setFrame: CGRectMake(120, 180, 100, height)];
        [self addSubview:titleLabel];
        [titleLabel release];

    }
    return self;
}


- (void)drawRect:(CGRect)rect {

}


- (void)dealloc {
    [super dealloc];
    [spinner release];
}


@end

2 个答案:

答案 0 :(得分:0)

当您开始加载过程时,可以将行设置为“关闭”,将它们设置为透明。


// Setting transparent
tableView.separatorColor = [UIColor clearColor];

当您删除加载UIView时,您可以更改所需的颜色。


// Setting the desired color
tableView.separatorColor = [UIColor grayColor];

另一种方法是在没有数据显示的情况下将表视图设置为隐藏,并且当您显示第一行或更多行时,将表视图设置为不隐藏。

我希望这会对你有所帮助!

干杯,
VFN

答案 1 :(得分:-1)

问题是viewController中的self.view直接分配了tableView。解决方案是为self.view指定一个UIView,并在self.view之上添加tableView和自定义UIView。

这是来自viewController的示例代码。

- (void)loadView {


    UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    //tableView
    self.tableView = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease];
    //set the rowHeight once for performance reasons.
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 75;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);                    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.autoresizesSubviews = YES;

    [aView addSubview:tableView];

    //set the rowHeight once for performance reasons.
    self.view = aView;
    [aView release];

}


- (void)viewDidLoad {
    [super viewDidLoad];

    loadingView = [[LoadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:loadingView];


    //fetch productsArray
    [self productListRequestStart];

}