iphone - UITableViewCells出现在backgroundView后面

标签 iphone uitableview uiimageview

我在将 UITableView 的背景设置为 UIImageView 时遇到问题(请参阅下文了解我这样做的原因),设置 View 后,它工作正常,并与 UITableView 一起滚动,但它隐藏了 UITableView 的元素表格 View 。

我需要一个 UIImageView 作为 UITableView 的背景。我知道以前已经回答过这个问题,但答案是使用:

[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];

或者类似的东西(我需要使用):

UIImageView *background = [MainWindow generateBackgroundWithFrame:tableView.bounds];
[tableView addSubview:background];
[tableView sendSubviewToBack:background];

我需要使用后者的原因是因为我的generateBackgroundWithFrame方法,它需要一个大图像,并在该图像周围绘制指定尺寸的边框,并剪辑图像的其余部分:

+ (UIImageView *) generateBackgroundWithFrame: (CGRect)frame {
    UIImageView *background = [[[UIImageView alloc] initWithFrame:frame] autorelease];
    background.image = [UIImage imageNamed:@"globalBackground.png"];
    [background.layer setMasksToBounds:YES];
    [background.layer setCornerRadius:10.0];
    [background.layer setBorderColor:[[UIColor grayColor] CGColor]];
    [background.layer setBorderWidth:3.0];

    return background;
}

请注意:我知道这可能会对性能产生不良影响,但我没有资源来为应用程序中的每个潜在屏幕制作这些图像。请不要用无意识的“你不应该这样做”的回答来回答这个问题。我知道这可能是错误的做法。

如何显示我的 UITableView 控件元素?我的代表是否做错了什么?这是一个简化版本:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = 
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                initWithFrame:CGRectMake(20, 20, 261, 45)
                reuseIdentifier:CellIdentifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        UIImage *rowBackground;         
        NSString *imageName = @"standAloneTVButton.png";    
        rowBackground = [UIImage imageNamed:imageName];     

        UITextView *textView = 
                [[UITextView alloc] initWithFrame:CGRectMake(0, 5, 300, 200)];

        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = [UIColor blackColor];
        textView.font = [UIFont fontWithName:@"Helvetica" size:18.0f];

        Purchase *purchase = 
                [[PurchaseModel productsPurchased] objectAtIndex:indexPath.row];

        textView.text = [purchase Title];

        selectedTextView.text = textView.text;


        UIImageView *normalBackground = 
                [[[UIImageView alloc] init] autorelease];

        normalBackground.image = rowBackground;

        [normalBackground insertSubview:textView atIndex:0];

        cell.backgroundView = normalBackground;
        [textView release];
    }

    return cell;
}

最佳答案

所以到目前为止这个问题的答案是向背景 View 添加一个标签,然后执行以下操作:

- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)path {
    [tableView sendSubviewToBack:[tableView viewWithTag:BACKGROUNDVIEW_TAG]];
}

我不太喜欢这个,因为我对每个单元格都这样做,但我似乎找不到 allCellsDidLoadForTableView: (UITableView *)tableView某种方法。

关于iphone - UITableViewCells出现在backgroundView后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2664596/

相关文章:

iPhone UIControl 和 subview

ios - UISearchBar 上方的线

ios - 如何处理 UITableViewController 中的点击单元格

swift - 如何根据图像调整 UIImaveView 的大小?

ios:在 Collection View 单元格中重叠图像

ios - 如何创建一个允许对 UIImageViews 进行分页和缩放的 UIScrollView?

ios - 垂直对齐 UILabel 中的文本 (xcode)

iphone - iOS 5.1 中的 ARC 和 Storyboard 未释放内存

iphone - 让 iPhone 振动

ios - 一些不需要的 UIImageViews 在尝试将它们设置为 UITableView 的边框时消失