How do I code a specific array element in section (GroupBy)?
in editingStyle, lists.remove(at: indexPath.row) moved the wrong element.
Help..
what is the difference between following two ones code?
- sections[indexPath.section].trades.remove(at: indexPath.row)
and
- self.trades.removeAll(where : { $0.id == sections[indexPath.section].trades[indexPath.row].id }) // MY MISTAKE: need .id at the end of course
You have to understand what are your data structures.
- self.trades is the original array of Trade with all the original items
- sections is an array of sectionBySymbol, each having an array of Trade, which is a subset of self.trades
- sections is computed from self.trades grouping by symbol; its trades include only those items in self.trades that match their symbol.
In
sections[indexPath.section].trades.remove(at: indexPath.row)
- you remove an item from a given section trades ; but you leave
self.trades
untouched. - Hence, if you recompute sections with grouping by symbol, you do this from the original untouched self.trades array. Hence, any item you may have removed in a section will reappear.
In
self.trades.removeAll(where : { $0.id == sections[indexPath.section].trades[indexPath.row].id }) // .id at the end
$0.id is the id of a Trade item in self.trades ; sections[indexPath.section].trades[indexPath.row].id is the id of an item in the section from which you remove item.
- you remove item directly from self.trades (those which id matches the one of removed item in table : sections[indexPath.section].trades[indexPath.row])
- So now, if you recompute sections, the removed item is no more in self.trades and thus will not reappear in any section.
Note: things could become more clear if you changed the name of property:
struct sectionBySymbol {
var symbol: String
var sectionTrades: [Trade] // Renamed
}
Then
self.trades.removeAll(where : { $0.id == sections[indexPath.section].trades[indexPath.row].id })
becomes
self.trades.removeAll(where : { $0.id == sections[indexPath.section].sectionTrades[indexPath.row].id })
Hope that helps.