I am trying to use confirmationDialog to delete an item in a List.
But what happens is that the wrong item gets deleted. Why?
Here is my code:
struct MyView: View {
@State private var selectedUsers: MyModel?
@State var datas: [MyModel]
@State private var confirmDelete = false
var body: some View {
ScrollView(.vertical, showsIndicators: false, content:
{
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
.alert(isPresented: $confirmDelete) {
Alert(title: Text("title"),
message: Text("message"),
primaryButton: .destructive(Text("Delete")) {
self.delete(item: data)
},
secondaryButton: .cancel())
}
} .onDelete { (indexSet) in
self.datas.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
Post
Replies
Boosts
Views
Activity
I have list items in SwiftUI, and when I delete list items, but when I click the delete button, it delete item randomly, I want to delete seleted item, where is the mistake?
struct MyView: View {
@State private var selectedUsers: MyModel?
@State var datas: [MyModel]
@State private var confirmDelete = false
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
.confirmationDialog( "Are you sure ?",
isPresented: $confirmDelete,
titleVisibility: .visible
){
Button(role: .destructive) {
self.delete(item: data)
} label: {
Text("ok")
}
Button(role: .cancel) {
} label: {
Text("cancel")
}
}
} .onDelete { (indexSet) in
self.datas.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
I have list items in SwiftUI, and when I delete list items I want to delete after alert menu, like
"do want to delete your list items, ""yes" or "no"
is it possible?
struct MyView: View {
@State private var selectedUsers: MyModel?
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
} .onDelete { (indexSet) in
self.datas.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
I have a simple app in SwiftUI, and I try to delete all list items with context menu , when I click context menu button, I want to remove all items, is it possible?
struct MyView: View {
@State private var selectedUsers: MyModel?
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
} .onDelete { (indexSet) in
selectedUsers.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
model:
struct MyModel: Identifiable, Hashable, Codable {
var id = UUID().uuidString
var name: String
}
var datas = [
MyModel(name: "david"),
MyModel(name: "marry"),
]
I am searching for a long time, how we can use custom emoji in SwiftUI, for a chat app, there is some example on internet but they are not for custom emoji, is it possible in SwiftUI?
I have a simple app in SwiftUI, and I try to delete list items in app , project is working, but still list items not able to delete, I do not know what I did not put in my codes, any idea will be appreciated.
struct MyView: View {
@State private var selectedUsers: MyModel?
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
} .onDelete { (indexSet) in
selectedUsers.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
model:
struct MyModel: Identifiable, Hashable, Codable {
var id = UUID().uuidString
var name: String
}
var datas = [
MyModel(name: "david"),
MyModel(name: "marry"),
]
I have example of swiftUI project, and it is throw an error
as a "Value of type 'ContentView' has no member 'restaurants'" for
self.restaurants.remove(atOffsets: indexSet) }
line of code, I do not know what I missed, any idea?
import SwiftUI
struct ContentView: View {
@State private var selectedRestaurant: Restaurant?
var body: some View {
List {
ForEach(restaurants) { restaurant in
BasicImageRow(restaurant: restaurant)
.contextMenu {
Button(action: {
// mark the selected restaurant as check-in
self.checkIn(item: restaurant)
}) {
HStack {
Text("Check-in")
Image(systemName: "checkmark.seal.fill")
}
}
Button(action: {
// delete the selected restaurant
self.delete(item: restaurant)
}) {
HStack {
Text("Delete")
Image(systemName: "trash")
}
}
Button(action: {
// mark the selected restaurant as favorite
self.setFavorite(item: restaurant)
}) {
HStack {
Text("Favorite")
Image(systemName: "star")
}
}
}
.onTapGesture {
self.selectedRestaurant = restaurant
}
.actionSheet(item: self.$selectedRestaurant) { restaurant in
ActionSheet(title: Text("What do you want to do"), message: nil, buttons: [
.default(Text("Mark as Favorite"), action: {
self.setFavorite(item: restaurant)
}),
.destructive(Text("Delete"), action: {
self.delete(item: restaurant)
}),
.cancel()
])
}
}
.onDelete { (indexSet) in
self.restaurants.remove(atOffsets: indexSet)
}
}
}
private func delete(item restaurant: Restaurant) {
if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {
self.restaurants.remove(at: index)
}
}
private func setFavorite(item restaurant: Restaurant) {
if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {
self.restaurants[index].isFavorite.toggle()
}
}
private func checkIn(item restaurant: Restaurant) {
if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {
self.restaurants[index].isCheckIn.toggle()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct BasicImageRow: View {
var restaurant: Restaurant
var body: some View {
HStack {
Image(restaurant.image)
.resizable()
.frame(width: 40, height: 40)
.cornerRadius(5)
Text(restaurant.name)
if restaurant.isCheckIn {
Image(systemName: "checkmark.seal.fill")
.foregroundColor(.red)
}
if restaurant.isFavorite {
Spacer()
Image(systemName: "star.fill")
.foregroundColor(.yellow)
}
}
}
}
model:
struct Restaurant: Identifiable {
var id = UUID()
var name: String
var image: String
var isFavorite: Bool = false
var isCheckIn: Bool = false
}
var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"),
Restaurant(name: "Homei", image: "homei"),
Restaurant(name: "Teakha", image: "teakha"),
Restaurant(name: "Cafe Loisl", image: "cafeloisl"),
]
I have small restaurans data in content view, but I want to use this data shortly inside of the homeview,
I am typing to use
@State var restaurants: [ Restaurant]
but in other view I have HomeView, it is throw an error like
Cannot convert value of type '[Restaurant].Type' to expected argument type '[Restaurant]'
for
HomeView( restaurants: [Restaurant])
line of code, I do not know what I missed? Any idea?
struct HomeView: View {
@State var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"),
Restaurant(name: "Homei", image: "homei"),
Restaurant(name: "Teakha", image: "teakha"),
Restaurant(name: "Cafe Loisl", image: "cafeloisl"), ]
}
I want to use onboarding screen in my project, and it is work but I want to use it just once time for app, I do not know how I will do it, is there any way?
struct ContentView: View {
@State private var onboardinDone = false
var data = OnboardingData.data
var body: some View {
Group {
if !onboardinDone {
OnboardingView(data: data, doneFunction: {
print("done onboarding")
})
} else {
MainScreen()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I have simple chat app, and I want to use simple emoji in this chat app, my emoji is located in
EmojiView
an when I click the emoji button, I want to add in live chat, but I do not know how I will forward for it, I was look at many resources but I did not find any example on internet, is it possible to do it?
import SwiftUI
struct MessageDetailsView: View {
var body: some View {
HomeMessageDetails()}}
struct MessageDetailsView_Previews: PreviewProvider {
static var previews: some View {
MessageDetailsView()
}}
struct HomeMessageDetails : View {
@State var message = ""
@StateObject var allMessages = Messages()
@State private var emojiData = false
var body: some View{
ZStack {
VStack{
ScrollView(.vertical, showsIndicators: false, content: {
ScrollViewReader{reader in
VStack{
ForEach(allMessages.messages){message in
ChatBubble(message: message)
}
.onChange(of: allMessages.messages) { (value) in
if value.last!.chatMessages{
reader.scrollTo(value.last?.id)}}}
.padding([.horizontal,.bottom])}})
HStack {
HStack{
TextField("Message", text: self.$message)
Button(action: {
emojiData = true
}, label: {
Image("emoji")
}) .sheet(isPresented: $emojiData) {
EmojiView()
} }
.padding(.vertical, 10)
.padding(.horizontal)
Button(action: {
allMessages.messages.append(Message(id: Date(), message: message, chatMessages: true))
message = ""
}, label: {
Image("reply")
}) }
.padding(.horizontal)
}} }}
struct ChatBubble : View {
var message : Message
var body: some View{
HStack(alignment: .top,spacing: 10){
if message.chatMessages{
Text(message.message)
.foregroundColor(Color("black))
}
else{
Text(message.message)
.foregroundColor(.white) }}
.id(message.id)}}
struct Message : Identifiable,Equatable{
var id : Date
var message : String
var chatMessages : Bool
}
class Messages : ObservableObject{
@Published var messages : [Message] = []
init() {
let strings = ["Hii","Hello !!!!"]
for i in 0..<strings.count{
messages.append(Message(id: Date(), message: strings[i], chatMessages: i % 2 == 0 ? true : false))}}
func writeMessage(id: Date,message: String,chatMessages: Bool){
messages.append(Message(id: id, message: message, chatMessages: chatMessages))}}
struct EmojiView: View {
var body: some View {
Button(action: {
}, label: {
Image("smile_emoji")
}) }}
I ask this question before, it was answered but it is not work for many data, like when I click the any list items, all details pass name of "david", I want to pass data of user which I clicked, where I missed here?
struct ImageModel: Identifiable, Hashable {
var id = UUID().uuidString
var name: String
}
var datas = [
ImageModel(name: "davis"),
ImageModel(name: "carry"),
ImageModel(name: "maria"),
]
struct ImageRowView: View {
var data: ImageModel
var body: some View {
NavigationLink(destination: ImageDetailsView(data: ImageModel)){
HStack{}
}}}
struct ImageDetailsView: View {
var body: some View {
ImageDetails(data: ImageModel(name: "davis"))
}
}
struct ImageDetailsView_Previews: PreviewProvider {
static var previews: some View {
ImageDetailsView()
}
}
struct ImageDetails : View {
var data: ImageModel
var body: some View{
VStack{
Text(data.name)
}
}
I have a problem when try to pass data from one view to another view, it is throw an error for
ImageDetails()
as a "Missing argument for parameter 'data' in call"
I do not know what I missed?
struct ImageModel: Identifiable, Hashable {
var id = UUID().uuidString
var name: String
}
var datas = [
ImageModel(name: "davis"),
]
struct ImageRowView: View {
var data: ImageModel
var body: some View {
NavigationLink(destination: ImageDetailsView(data: ImageModel)){
HStack{}
}}}
struct ImageDetailsView: View {
var body: some View {
ImageDetails()
}
}
struct ImageDetailsView_Previews: PreviewProvider {
static var previews: some View {
ImageDetailsView()
}
}
struct ImageDetails : View {
var data: ImageModel
var body: some View{
VStack{
Text(data.name)
}
}
I have project in SwiftUI 2.0 but when I update to SwiftUI 3.0 it is throw an error for
windows
as a
windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
any idea?
.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)
I have a project, after update it to swiftUI 3.0, it is throw an error for
.animation(.spring())
as animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead, any idea?
@Binding var display: Bool
private var background: some View {
Color.black
.fillParent()
.opacity(0.6)
.animation(.spring())
}
I want to use many buttons like background have custom colors with grid view in swiftUI, is it possible? I know how to use grid layout for images, but I have confused to use for custom colors.