在 swift 中,我如何从我的 tableview 中删除一个对象?

In swift, how can i remove an object from my tableview?

我有一个使用此字典的数据,名为:来自 class Donnees

的 listDuJour
class AlimentObject {
    var id = UUID().uuidString
    var nomAliment = "Nouvel aliment"
    var poids : Float = 100
    var calories : Float = 0
    var proteines : Float = 0
    var lipides : Float = 0
    var glucides : Float = 0
    var aRetinol : Float = 0
    var aBetacarotene : Float = 0
    var vitamineC : Float = 0
    var vitamineB1 : Float = 0
    var calcium : Float = 0
    var omega3 : Float = 0
    var comments : String = ""
    var premierBooleen: Bool
    var optimize: Bool
    

    
    init(nomAliment: String, poids: Float, calories: Float, proteines: Float, lipides: Float, glucides: Float, aRetinol: Float, aBetacarotene: Float, vitamineC: Float, vitamineB1: Float, calcium: Float, omega3: Float, comments:String, premierBooleen: Bool, optimize: Bool) {
        
        self.nomAliment = nomAliment
        self.poids = poids
        self.calories = calories
        self.proteines = proteines
        self.lipides = lipides
        self.glucides = glucides
        self.aRetinol = aRetinol
        self.aBetacarotene = aBetacarotene
        self.vitamineC = vitamineC
        self.vitamineB1 = vitamineB1
        self.calcium = calcium
        self.omega3 = omega3
        self.comments = comments
        self.premierBooleen = false
        self.optimize = false
    }
    
    convenience init() {
        self.init(nomAliment: "Nouvel aliment", poids: 100, calories: 0, proteines: 0, lipides: 0, glucides: 0, aRetinol: 0, aBetacarotene: 0, vitamineC: 0, vitamineB1: 0, calcium: 0, omega3: 0, comments: "", premierBooleen: false, optimize: false)
    }
}

let begin01 = "11 09 2020"
let dateFormatter01 = DateFormatter()
dateFormatter01.dateFormat = "dd MM yyyy"
let date01 = dateFormatter01.date(from: begin01)!

let begin02 = "12 09 2020"
let dateFormatter02 = DateFormatter()
dateFormatter02.dateFormat = "dd MM yyyy"
let date02 = dateFormatter02.date(from: begin02)!

let begin03 = "15 09 2020"
let dateFormatter03 = DateFormatter()
dateFormatter03.dateFormat = "dd MM yyyy"
let date03 = dateFormatter03.date(from: begin03)!


func dateSimplificator(with date: Date) -> Date {
    return Calendar.current.startOfDay(for: date)
}

class Donnees {
        
        static var AlimentListWithSections = [[AlimentObject]]()
        
        static let list1 = [
            AlimentObject(nomAliment: "Blanc de poulet", poids: 100, calories: 108, proteines: 24, lipides: 1, glucides: 0, aRetinol: 8, aBetacarotene: 0, vitamineC: 0, vitamineB1: 0.08, calcium: 8, omega3: 0.03, comments: "sdsdsdsdsd sdsddsdsd", premierBooleen: false, optimize: false),
            AlimentObject(nomAliment: "Avocat", poids: 100, calories: 147, proteines: 1.76, lipides: 13.80, glucides: 1.57, aRetinol: 0, aBetacarotene: 55, vitamineC: 5.49, vitamineB1: 0.05, calcium: 11.2, omega3: 0.08, comments: "aaaaaaaaa sdsddsdsd", premierBooleen: false, optimize: false),
            AlimentObject(nomAliment: "Amandes", poids: 100, calories: 634, proteines: 21, lipides: 53, glucides: 8, aRetinol: 8, aBetacarotene: 1, vitamineC: 0.8, vitamineB1: 0.18, calcium: 248, omega3: 0.06, comments: "fffffff sdsddsdsd", premierBooleen: false, optimize: false)
            ]
        
        static let list2 = [
            AlimentObject(nomAliment: "Poivron Jaune", poids: 80, calories: 29.2, proteines: 1, lipides: 5.42, glucides: 0.21, aRetinol: 0, aBetacarotene: 120, vitamineC: 184, vitamineB1: 0.03, calcium: 11, omega3: 0.02, comments: "lllllllssss", premierBooleen: false, optimize: false),
            AlimentObject(nomAliment: "Saumon cru", poids: 100, calories: 192, proteines: 20.5, lipides: 12.2, glucides: 0, aRetinol: 8, aBetacarotene: 0, vitamineC: 1.6, vitamineB1: 0.21, calcium: 8, omega3: 1.79, comments: "eeeeeeee", premierBooleen: false, optimize: false)
            ]
        
        static let list3 : [AlimentObject] = [
            AlimentObject(nomAliment: "Avocat", poids: 100, calories: 147, proteines: 1.76, lipides: 13.80, glucides: 1.57, aRetinol: 0, aBetacarotene: 55, vitamineC: 5.49, vitamineB1: 0.05, calcium: 11.2, omega3: 0.08, comments: "aaaaaaaaa sdsddsdsd", premierBooleen: false, optimize: false),
            ]
        
        static let list4 : [AlimentObject] = [
            AlimentObject(nomAliment: "Amandes", poids: 100, calories: 108, proteines: 21, lipides: 53, glucides: 8, aRetinol: 8, aBetacarotene: 1, vitamineC: 0.8, vitamineB1: 0.18, calcium: 248, omega3: 0.06, comments: "fffffff sdsddsdsd", premierBooleen: false, optimize: false)
            ]
        
        static let list5 : [AlimentObject] = []
    
        static let listVide : [AlimentObject] = []

        static let listDuJour = [list1, list2, list3, list4, list5]
    static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h", "Déjeuner", "Collation 16h", "Diner"]

    static func removeInstance(menuDuJour: [[AlimentObject]], attr1: String, attr2: Float) -> [[AlimentObject]] {
        let ListDuJourFiltree = menuDuJour.map {
            [=11=].filter { !([=11=].nomAliment == attr1 && [=11=].poids == attr2) }
        }
        return ListDuJourFiltree
    }
}

我如何从 listDuJour 的第二部分中删除名为“Poivron Jaune”且 poids =“80”的 Aliment 对象? 我有 2 个参数来搜索我的 AlimentObject:名称和 poid。 并且解决方案不应该删除数组中的空部分。

要删除 Sample(我的示例结构的名称)的实例,您可以执行以下操作。

struct Sample {
    var attr1: String
    var attr2: String
}

let list1: [Sample] = [
    .init(attr1: "alasdfj", attr2: "asd"),
    .init(attr1: "qwer", attr2: "upoipp"),
    .init(attr1: "yxcv", attr2: "gfhj")
]
let list2: [Sample] = [
    .init(attr1: "Poviron Jaune", attr2: "80")
]

let list = [list1, list2]

let resultList = list.map {
    [=10=].filter { !([=10=].attr1 == "Poviron Jaune" && [=10=].attr2 == "80") }
}

如果你想删除 resultList 中的空数组,你也可以将最后一部分更改为:

let resultList = list.map {
    [=11=].filter { !([=11=].attr1 == "Poviron Jaune" && [=11=].attr2 == "80") }
}.filter { !([=11=].isEmpty) }

(如果子数组中的所有实例都已删除,则会出现空数组)

因此您应该能够执行以下操作以摆脱 listDuJour 中所需的元素:

static func removeInstance(attr1: String, attr2: String) {
    listDuJour = listDuJour.map {
        [=12=].filter { !([=12=].nomAliment == attr1 && [=12=].poids == attr2) }
    }.filter { !([=12=].isEmpty) }
}

或者如果你想保留空数组(空部分):

static func removeInstance(attr1: String, attr2: String) {
    listDuJour = listDuJour.map {
        [=13=].filter { !([=13=].nomAliment == attr1 && [=13=].poids == attr2) }
    }
}

请注意,您必须在删除元素后调用 tableView.reloadData()