vrthmy5312
vrthmy5312

Reputation: 107

Move subview of a cell to the front swift

I have a cell with a button on it. When I press the button, I start an animation indicating something is preparing. I do this within the @IBAction function like so: (this is in my custom tableViewCell function).

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

I define this shape within the @IBAction as the process should repeat if I press the button again

However, because only the necessary cells displayed on the device are loaded in one chunk in the cellForRowAt function for the tableView, my animation repeats every few cells if I scroll down while the animation is loading.

What I have done so far is append to a list of all the previously pressed buttons in my by defining a function and calling it in the @IBAction function of the button like so:

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!)
        }
    }
}

Then, in my cellForRowAt function, I simply make a reverse operation, which checks if the current index in the list is the same as any value in the already selected ones.

Having all of these filtered out, I now only have a list of the non-selected ones.However, I do not know what to do now.

Weirdly, cell.bringSubview(tofront: cell.cellText) cell.bringSubview(tofront: cell.buttonText) does NOT change the order of the subviews. What would do I do now? Is it possible that CAShapeLayer() is not regarded as a subview, but only a layer?

Thank you in advance!

Upvotes: 0

Views: 761

Answers (1)

Max Rogers
Max Rogers

Reputation: 914

Weirdly, cell.bringSubview(tofront: cell.cellText) cell.bringSubview(tofront: cell.buttonText) does NOT change the order of the subviews. What would do I do now?

bringSubview(tofront:) only works with direct subviews. Traditionally your cellText and buttonText are subviews to cell.contentView.

so try

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

Is it possible that CAShapeLayer() is not regarded as a subview, but only a layer?

Yes, CAShapeLayer inherits from CALayer and only regarded as a layer to its view and will likely need to be updated via layoutSubviews() or draw()


Saw those nested for loops and if statements, thought I'd offer a way to clean that up a bit.

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!))
    }
}

Upvotes: 2

Related Questions