вы можете определить протокол на collectionViewCell
и запустить его, когда кнопка addToCard
нажмет на каждую ячейку, и в вашем viewController
орудии делегата:
protocol CollectionViewCellDelegate: class {
func addtoCard(_ cell: TrendingProductsCVCell)
}
class TrendingProductsCVCell: UITableViewCell {
weak var delegate: CollectionViewCellDelegate?
// ... the rest of your code
@IBAction func addToCardButtonClicked(_ sender: UIButton) { //action of ***addToCard***
delegate?.addToCard(self)
}
}
class viewController: CollectionViewCellDelegate { //and what you want to implement
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = trendingProductCV.dequeueReusableCell(withReuseIdentifier: "TrendingProductsCVCell", for: indexPath) as? TrendingProductsCVCell
cell.delegate = self
// ... the rest of your code
return cell
}
func addToCard(cell: TrendingProductsCVCell) {
//do what you want with your cell and its content
}
}
As you point out, you are giving up a lot because ActiveResource does not have associations in the sense that ActiveRecord does.
You have already found the answer to question #1. As for question #2, your ActiveRecord model Article should behave just fine when configured with a "belongs_to" association to an ActiveResource model. That is Aritcle.find(:first).author should return the person object you want.
Я предполагаю одну возможность для № 1, предполагая, что я могу получить любой из него работа, должен сделать это:
class Person < ActiveResource::Base
self.site = "http://api.people.mypublisher.com/"
def articles
Article.find(:all, :conditions => { :person_id => self.id })
end
def add_article(article)
article.person_id = self.id
end
end
Но это теряет многое из какой has_many
предложения.