Swiftui: Make Remaining Area of List Item Detect Clicks

SwiftUI: Detect click on already selected list item

there are a number of ways to do what you want,
try this with .onTapGesture{..}

    List (selection: $selection) {
ForEach(listItems, id: \.id) { item in
Text(item.text)
.tag(item.id)
.padding(10)
.listRowInsets(.init(top: 0,leading: 0, bottom: 0, trailing: 0))
.frame(minWidth: 111, idealWidth: .infinity, maxWidth: .infinity, alignment: .leading)
.onTapGesture {
print("----> " + item.text)
}
}
}

or you can use a Button instead of onTapGesture;

    List (selection: $selection) {
ForEach(listItems, id: \.id) { item in
Button(action: { print("----> " + item.text) }) {
Text(item.text)
}
.padding(10)
.listRowInsets(.init(top: 0,leading: 0, bottom: 0, trailing: 0))
.tag(item.id)
.frame(minWidth: 111, idealWidth: .infinity, maxWidth: .infinity, alignment: .leading)
}
}

EDIT: full test code:

This is the code I used in my tests:

import SwiftUI

@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}

struct NavItem {
let id: String
let text: String
}

struct ContentView: View {
@State private var listItems: [NavItem] = [NavItem(id: "1", text: "1"),
NavItem(id: "2", text: "2"),
NavItem(id: "3", text: "3"),
NavItem(id: "4", text: "4")]
@State private var selection: String?

var body: some View {
List (selection: $selection) {
ForEach(listItems, id:\.id) { item in
Button(action: { print("----> " + item.text) }) {
Text(item.text)
}
.padding(10)
.listRowInsets(.init(top: 0,leading: -5, bottom: 0, trailing: -5))
.frame(minWidth: 111, idealWidth: .infinity, maxWidth: .infinity, alignment: .leading)
.tag(item.id)
.background(Color.red) // <--- last modifier
}
}
}
}

SwiftUI Clicking on one Row in a List, sets every row on that list to true

There's only one isChecked for the entire list. Each row is using the same variable and so when one of them is tapped, all of them change.

Based on the code it looks like each item in the list has an isDone member. Try replacing self.isChecked with a.isDone for the Image name. Then try changing the button action from self.toggle to { a.isDone.toggle() }

So you're List would look like the following.

List(listItems) { a in
Button(action: {
a.isDone.toggle()
}){
HStack {
Image(systemName: a.isDone ? "checkmark.circle.fill" : "circle")
Text(a.text)
}
}
}.offset(y: -50)

SwiftUI: I have a list which when I click on an object I want to go into a detail view where I can select or deselect the current object

When creating the List you can use ForEach to access the index and then pass to CardView

 List {
ForEach(0..<playerList.playerCards.count) { index in
NavigationLink(destination: CardView(carddetail: self.playerList.playerCards[index].name,
index: index)) {
Text("\(self.playerList.playerCards[index].name)")
}
}.navigationBarTitle("Card detail").font(.body)
}

How to detect taps on a List cell row in SwiftUI?

Add a Button and entire cell is tappable now:

VStack {
Button(action: {

print("Tapped")
}) {
Text("Hello world")
}
}

SwiftUI sidebar list does not register first click

It is needed to add selection for List, like below

@State var current = Set<String>()
var body: some View {
List(items, id: \.self, selection: $current) { item in
NavigationLink(destination: DetailView(selection: item)) {
Text(item)
}
}
.listStyle(SidebarListStyle())
}


Related Topics



Leave a reply



Submit