在UITableViewCell里面的UIView绘图没有出现

时间:2016-09-09 19:02:28

标签: uitableview uiview

我正在使用PaintCode将一些图像添加到UITableViewCell,并且PaintCode图像(在UIView内绘制)不能可靠地显示在屏幕上。

有一个水平堆栈视图,包含3个水平堆栈视图,其中一些可能会隐藏,具体取决于模型。如果其中一个内部堆栈视图可能设置为hidden=YES,则该堆栈视图中的UIView将不会始终显示,即使该视图存在且堆栈视图正在为其腾出空间。

向您展示可能更容易:

xcode view inspector

正如您在“视图层次结构”检查器屏幕截图中看到的那样,4旁边有一个用于栗色六边形的空间,但它没有出现。如果我选择单元格,您可以在单元格突出显示时始终看到六边形,然后如果我将新视图控制器推入并弹出堆栈,有时六边形将保留(如下面的单元格中所示)。但是,在tableview的初始加载时,六边形几乎总是丢失。永远不会在.hidden本身上直接访问UIView属性,只会在包含UIStackView的<{1}}上。

控制加载UITabelViewCell的代码如下(debtStack包含有问题的六边形):

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

CardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CardCustomCell"];

if (cell == nil) {
    cell = [[CardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CardCustomCell"];
}

[cell cellCard:self.rowArray[indexPath.section][indexPath.row]];
cell.cellButton.hidden = YES;

return cell;

}

- (void)cellCard:(Card *)card {

if ([[BlackListSingleton theBlackList] cardExists:card]) {
    [self setBadge:1];
} else if ([[WhiteListSingleton theWhiteList] cardExists:card]) {
    [self setBadge:2];
} else {
    [self setBadge:0];
}

self.typeLabel.text = [Card stringForCardType:card.cardType];
self.nameLabel.text = card.cardName;

if (card.coinCost) {
    self.coinStack.hidden = NO;
    self.coinCost.text = card.coinCost;
} else {
    self.coinStack.hidden = YES;
}
if (card.potionCost) {
    self.potionStack.hidden = NO;
    self.potionCost.text = card.potionCost;
} else {
    self.potionStack.hidden = YES;
}
if (card.debtCost) {
    self.debtStack.hidden = NO;
    self.debtCost.text = card.debtCost;
} else {
    self.debtStack.hidden = YES;
}

[self cardTypeImage:card.cardType];

}

1 个答案:

答案 0 :(得分:0)

好的,这不是最优雅的解决方案。您可以通过将绘图方法添加到数组并根据需要调用来改进此答案,但这是我对如何解决此问题的第一次迭代。

首先,我们在storyboard中创建一个原型单元,将您的uitableviewcell / collectionviewcell子类化,确保您记得在您的身份检查器中添加该类。

我的uitableviewcell子类看起来像这样

import UIKit

class CategoriesTableViewCell: UITableViewCell {

    @IBOutlet weak var categoryImage: UIImageView!

    @IBOutlet weak var categoryView: DesignIconView!
    @IBOutlet weak var subtitleLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!

    let cView = DesignIconView()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)


    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func configure(forRow row : Int, title : String, subTitle: String){

        cView.drawForRow = row
        cView.frame = CGRect(x: 0, y: 0, width: categoryView.frame.width, height: categoryView.frame.height)
        cView.backgroundColor = UIColor.clear
        categoryView.addSubview(cView)

        titleLabel.text  = title
        subtitleLabel.text = subTitle

    }



}

接下来,我在原型单元格中添加了一个视图,创建了一个uiview子类,并在身份检查器中再次设置了该类

我的uiview子类看起来像这样......你可以通过将绘图方法添加到数组中来轻松改进这一点,但为了简单起见,请使用switch语句。

import UIKit

class DesignIconView: UIView {

    private var _drawForRow : Int!

    var drawForRow: Int! {
        set {

            _drawForRow = newValue
        } get {

            if _drawForRow == nil {
                _drawForRow = 0
            }
           return _drawForRow
        }
    }

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code

        guard  _drawForRow != nil else {
            return _drawForRow = 0
        }
        // marketing, business, lifestyle, music, tech, video, design
        switch _drawForRow {
        case 0:
            CategoryIcons.drawMarketingIcon(frame: self.bounds, resizing: .aspectFit)
        case 1:
            CategoryIcons.drawBusinessIcon(frame: self.bounds, resizing: .aspectFit)
        case 2:
            CategoryIcons.drawLifeStyleIcon(frame: self.bounds, resizing: .aspectFit)
        case 3:
            CategoryIcons.drawMusicIcon(frame: self.bounds, resizing: .aspectFit)
        case 4:
            CategoryIcons.drawTechIcon(frame: self.bounds, resizing: .aspectFit)
        case 5:
            CategoryIcons.drawVideoIcon(frame: self.bounds, resizing: .aspectFit)
        case 6:
            CategoryIcons.drawDesignIcon(frame: self.bounds, resizing: .aspectFit)


        default:
            CategoryIcons.drawMusicIcon(frame: self.bounds, resizing: .aspectFit)

        }




        print("Drawing icon")
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

}

最后在我的viewcontroller中

func configureCategoriesCells( cell: CategoriesTableViewCell, indexPath : IndexPath) {

    let categoryLabels = ["A", "B", "C", "D", "E", "F", "G"]
    let subCategoryLabels = ["subtitle","subtitle","subtitle,"subtitle", "subtitle", "subtitle"]

    cell.configure(forRow: indexPath.row, title: categoryLabels[indexPath.row], subTitle: subCategoryLabels[indexPath.row])
}

结果是根据需要绘制图标:)

让我知道你是怎么做到的