【问题标题】:Move subview of a cell to the front swift将单元格的子视图快速移动到前面
【发布时间】:2018-03-27 17:26:45
【问题描述】:

我有一个带有按钮的单元格。当我按下按钮时,我会启动一个动画,指示正在准备的东西。我在 @IBAction 函数中这样做:(这是在我的自定义 tableViewCell 函数中)。

@IBAction func playNowTapped(_ sender: UIButton) {
    let loadingShape = CAShapeLayer()
    //Some animating of the shape
}

我在@IBAction 中定义了这个形状,因为如果我再次按下按钮,这个过程应该重复

但是,因为只有设备上显示的必要单元格在 tableView 的cellForRowAt 函数中的一个块中加载,所以如果我在动画加载时向下滚动,我的动画每隔几个单元格就会重复一次。

到目前为止,我所做的是通过定义一个函数并在按钮的@IBAction 函数中调用它,将其附加到我之前按下的所有按钮的列表中,如下所示:

func findCell() {
    //Iterate tableView list and compare to current cellText
    for value in list {
        if value == cellText.text {
             //If found, checking if value is already stored in pressedBefore
             for selected in pressedBefore {
                 if selected == value { return }
             }
             alreadyPlay.append(song: cellText.text!)
        }
    }
}

然后,在我的cellForRowAt 函数中,我简单地进行反向操作,检查列表中的当前索引是否与已选择的索引中的任何值相同。

过滤掉所有这些后,我现在只有一个未选择的列表。但是,我现在不知道该怎么办。

奇怪的是,cell.bringSubview(tofront: cell.cellText) cell.bringSubview(tofront: cell.buttonText) 不会改变子视图的顺序。我现在该怎么办?会不会CAShapeLayer()不被视为子视图,而只是一个层?

提前谢谢你!

【问题讨论】:

    标签: ios swift uitableview cell layer


    【解决方案1】:

    奇怪的是,cell.bringSubview(tofront: cell.cellText) cell.bringSubview(tofront: cell.buttonText) 不会改变子视图的顺序。我现在该怎么办?

    bringSubview(tofront:) 仅适用于直接子视图。传统上,您的 cellText 和 buttonText 是 cell.contentView 的子视图。

    所以试试

    cell.contentView.bringSubview(tofront: cell.buttonText)
    

    有没有可能CAShapeLayer()不被视为子视图,而是 只有一层?

    是的,CAShapeLayer 继承自 CALayer,仅被视为其视图的一个层,可能需要通过layoutSubviews()draw() 进行更新


    看到那些嵌套的 for 循环和 if 语句,我想我会提供一种方法来清理一下。

    func findCell() {
        //find list elements that match cell's text and ensure it hasn't been pressed before
        list.filter { $0 == cellText.text && !pressedBefore.contains($0) }.forEach {
            alreadyPlay.append(alreadyPlayed(song: LeLabelOne.text!, artist: leLabelThree.text!))
        }
    }
    
    // alternative using Sets
    func findCell() {
        let cellTextSet = Set(list).intersection([cellText.text])
    
        // find entries in cellTextSet that haven't been pressed before
        cellTextSet.subtract(Set(pressedBefore)).forEach {
            alreadyPlay.append(alreadyPlayed(song: LeLabelOne.text!, artist: leLabelThree.text!))
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      相关资源
      最近更新 更多