fatbobman / SwipeCell

SwipeCell is a SwiftUI library, used to achieve the left and right sliding menu effect similar to the iOS mail app.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to get the id of the swiped cell?

nickcom111 opened this issue · comments

I don't see any example in the demo on how to do this. Surely once you have swiped a cell you need to know which one has been swiped but I can't figure out how to do this

Each cellModifier has it's own id(UUID)
It' will post a notification when it was swiped.

                if currentCellID != cellID {
                    currentCellID = cellID
                    NotificationCenter.default.post(Notification(name: .swipeCellReset, object: cellID))
                }

all cell modifier will check if own cell.id == notification.id

        .onReceive(resetNotice){ notice in
            if cellID != notice.object as? UUID {
                resetStatus()
                currentCellID = notice.object as? UUID ?? nil
            }  
        }

fantastic. Thanks so much for the reply.
I'm new to swiftui and your package is amazing, but I'm still missing on part of the puzzle.

So now that I know the UUID for cell that was swiped (example currentCellID = '6AC81E07-E766-45A5-8B46-70EF38BB8021') how can I map that back to the List the swipeCell is attached to, so that I may determine which list item was swiped?

The code has been updated.
Now ,the cell status will be injected in cell content.
Cell content can use blow code to get the status from Environment.

HStack{
    Text("Cell Status:")
    Text(status.rawValue)
        .foregroundColor(.red)
        //get the cell status from Environment
        .transformEnvironment(\.cellStatus, transform: { cellStatus in
            let temp = cellStatus
            DispatchQueue.main.async {
                self.status = temp
            }
        })
}
.frame(maxWidth:.infinity,alignment: .center)
.frame(height:100)
.swipeCell(cellPosition: .both, leftSlot: slot, rightSlot: slot)

You can get more details from DemoShowStatus in Demo.

thanks Bob - very useful!! Love your work!

So let's say I have a list with 3 cells - how will I get the ID of the one that is swiped?

Example: in iOS mail app you can swipe to flag an email. Default SwipeCell package behaviour will allow me to visually flag the cell as 'green' color to indicate it has been flagged. But I must keep a note of this action in the database which means I need to know which cell was swiped. How to get the swiped cell?

Apologies if this is obvious, but I can't work it out :/

import SwiftUI
import SwipeCell

struct ContentView: View {
    
    struct Result {
        var id = UUID()
        var score: Int
    }
    
    let results = [Result(score: 8), Result(score: 5), Result(score: 10)]

    
    let button = SwipeCellButton(
        buttonStyle: .titleAndImage,
        title: "Mark",
        systemImage: "bookmark",
        titleColor: .white,
        imageColor: .white,
        view: nil,
        backgroundColor: .green,
        action: { },
        feedback: true
    )

    var slot:SwipeCellSlot{
        SwipeCellSlot(slots: [button])
    }

    var body: some View {
        VStack {
            ForEach(results, id: \.id) { result in
                Text("Result: \(result.score)")
                    .swipeCell(cellPosition: .both, leftSlot: slot, rightSlot: slot) //<<maybe some func here to expose the id of the swiped cell?
            }
            .frame(height: 70)
        }
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
struct ContentView: View {

    struct Result {
        var id = UUID()
        var score: Int
    }

    let results = [Result(score: 8), Result(score: 5), Result(score: 10)]

   
    var body: some View {
         VStack {
            ForEach(results, id: \.id) { result in
                //move button declare to here
                let button = SwipeCellButton(
                    buttonStyle: .titleAndImage,
                    title: "Mark",
                    systemImage: "bookmark",
                    titleColor: .white,
                    imageColor: .white,
                    view: nil,
                    backgroundColor: .green,
                    action: {
                        //do something
                        flag(id:result.id)
                    },
                    feedback: true
                )

              let slot:SwipeCellSlot  =  SwipeCellSlot(slots: [button])

                Text("Result: \(result.score)")
                    .swipeCell(cellPosition: .both, leftSlot: slot, rightSlot: slot) //<<maybe some func here to expose the id of the swiped cell?
            }
            .frame(height: 70)
        }
    }

}

lifesaver! 🥇