簡體   English   中英

在編輯模式下重新排序時更改UITableViewCell的背景顏色

[英]Change background color of UITableViewCell when reordering in editing mode

我正在開發一個包含UITableView的應用程序。 用戶唯一需要的是重新排序UITableView的內容。 在編輯模式下,tableview單元格的背景顏色變為透明。 在重新排序時,有沒有辦法保持tableview單元格的原始背景顏色?

解:

經過幾次額外搜索后,我找到了(簡單)解決方案。 通過將cell.backgroundView.backgroundcolor添加到willDisplayCell方法,問題得以解決。

iOS UI框架將使您的單元格透明,有各種各樣的場景。 其中一個是突出顯示/選定狀態,另一個是重新排序的情況。

重要的是要了解系統是如何做到的。 系統隱藏單元格背景( cell.backgroundViewcell.selectedBackgroundView ),並且對於單元格中的每個視圖,它將backgroundColor設置為透明顏色。 因此,不可能使用backgroundColor來保持單元格不透明。

最簡單的解決方案是添加一個帶有drawRect:的簡單UIView drawRect:它將用一種顏色填充單元格。

@interface MyColoredView : UIView

@property (nonatomic, strong, readwrite) UIColor *color;

@end

@implementation MyColoredView

- (void)setColor:(UIColor *)color {
    _color = color;

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [self.color set];

    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
    CGContextFillRect(UIGraphicsGetCurrentContext(), self.bounds);
}

@end

將它添加到您的單元格(可能不是contentView而是直接添加到單元格)並設置其框架以匹配單元格的邊界。

說你的細胞顏色是紅色

 [cell setBackgroundColor:[UIColor redColor]];

然后將tableview背景設置為相同。

 tableView.backgroundColor = [UIColor redColor];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM