Add support to Register Multiple cell at once.
learnwithgabbar opened this issue · 5 comments
Hi @techmehra
I'm not sure I see the point of that issue? What's the gain of adding a dedicated API to register multiple cells while you can already do it with a simple map
? It's as simple as this:
[myCellClass1.self, myCellClass2.self, myCellClass3.self].map(tableView.register(cellType:))
Not sure why we'd need a dedicated API when stdlib's map
already does the job 😉
I already used that approach but getting this error Cannot invoke 'register' with an argument list of type '(cellType: (BaseTableViewCell.Type))'
Ok, I just tried to look at it a bit further, and made some tests to try it.
The problem is, because of generics (and protocols applying to instances not meta-types), it's not possible to have an API of consistent types in an array.
-
If you have a mix of
NibReusable
andReusable
classes you would like to register all at once, you can't put them in the same array (even if the next points weren't applicable) because they are not the same meta-types, and because each would involve a different variant ofregister(cellType:)
(there are two overrides of this method, with different generic parameter constraints) -
Even if you had an array of cell types that all conformed to the same protocol (
NibReusable
orReusable
, one of the two), as it's a generic method on a Type with constraints, but it's an array of meta-types, not instances, and you can't apply the protocol constraints on the meta-type (meta-type don't conform to protocols, only types do, so it'sBaseTableViewCell
which conforms toNibReusable
, notBaseTableViewCell.Type
, problem which is solved by a generics —T: NibReusable
thencellType: T.Type
— in the current API, but can't be applicable to arrays.
So given that I'm not even sure that it would be possible to make a register(cellType:)
method that would accept a sequence of different cell types, as it wouldn't work well with the generics API, I can't see a solution for this. If you find one, please let me know, but given the way the API is heavily based on generics + meta-types + protocol constraints, I don't think that's even possible in Swift 4 today
I'm closing this issue now for lack of any possible solution in Swift 4. Feel free to re-open it if you find a magic way to implement it given those generics constraints.
Hello, @AliSoftware I have a question related to this question. What kind of disadvantages of making UITableViewCell
conform to Reusable
you know? This will let us reach a solution for this topic:
extension UITableViewCell: Reusable {
}
[
FirstTableViewCell.self,
SecondTableViewCell.self,
...
].forEach { tableView.register(cellType: $0) }
Also, we have to conform to Reusable only once, not for each UITableViewCell
subclass. Are there any topics where it was discussed already?