子类化 UIButton 来保存数据?
Subclass UIButton to save Data?
我想知道扩展 UIButton 以引用我的数据是否有任何问题?
例如,我有一个 table 视图,每当用户点击一个单元格时,我需要向用户提供该特定单元格的数据。
数据保存在一个数组中,数组索引保存在 UIButton 标记中,但随着我的数组更新,将提供错误的索引。所以我想做的是扩展一个 uibutton,然后有一个变量来保存我的模型。
这个想法很好,但由于我在 swift 方面并没有真正的经验,所以我想知道这样做的缺点和问题是什么。
您不需要将 index
保存为 Button 的标签。正如 Sneak 在评论中指出的那样,Subclassing UIButton 显然是一个非常糟糕的主意。在 UIComponent
中保存模型最重要的是灾难设计。
您可以通过多种方式进行。我觉得整洁的一个 :
第 1 步:
为您的自定义单元格创建一个 Class。让我们说 MyCollectionViewCell
。因为您的 Cell 中有一个按钮,所以您应该在 Cell 中创建 IBAction
个按钮。
class MyCollectionViewCell: UICollectionViewCell {
@IBAction func myButtonTapped(_ sender: Any) {
}
}
第 2 步:
让我们声明一个我们将用来与 tableView/CollectionView 的数据源通信的协议。
protocol MyCollectionViewCellProtocol : NSObjectProtocol {
func buttonTapped(for cell : MyCollectionViewCell)
}
第 3 步:
让我们在 MyCollectionViewCell
中创建一个 属性
weak var delegate : MyCollectionViewCellProtocol? = nil
在第 3 步之后,您的 MyCollectionViewCell class 应该看起来像
protocol MyCollectionViewCellProtocol : NSObjectProtocol {
func buttonTapped(for cell : MyCollectionViewCell)
}
class MyCollectionViewCell: UICollectionViewCell {
weak var delegate : MyCollectionViewCellProtocol? = nil
@IBAction func myButtonTapped(_ sender: Any) {
self.delegate?.buttonTapped(for: self)
}
}
第 4 步:
在您的 tableView 的 CellForRowAtIndexPath 或 CollectionView 的 sizeForItemAtIndexPath 中确认传递 ViewController 作为单元格的委托。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : MyCollectionViewCell = //deque your cell
(cell as! MyCollectionViewCell).delegate = self
}
第 5 步:
现在让你的ViewController确认协议
class YourViewController: UIViewController,MyCollectionViewCellProtocol {
第 6 步:
终于在你的 VC
中实现方法
func buttonTapped(for cell: MyCollectionViewCell) {
let indexPath = self.collectionView?.indexPath(for: cell)
//access array now
}
P.S:
抱歉,虽然我知道您正在使用 TableView,但我匆忙为 CollectionView 编写了代码,但委托非常相同:)我希望您能够理解其中的逻辑
我想知道扩展 UIButton 以引用我的数据是否有任何问题?
例如,我有一个 table 视图,每当用户点击一个单元格时,我需要向用户提供该特定单元格的数据。
数据保存在一个数组中,数组索引保存在 UIButton 标记中,但随着我的数组更新,将提供错误的索引。所以我想做的是扩展一个 uibutton,然后有一个变量来保存我的模型。
这个想法很好,但由于我在 swift 方面并没有真正的经验,所以我想知道这样做的缺点和问题是什么。
您不需要将 index
保存为 Button 的标签。正如 Sneak 在评论中指出的那样,Subclassing UIButton 显然是一个非常糟糕的主意。在 UIComponent
中保存模型最重要的是灾难设计。
您可以通过多种方式进行。我觉得整洁的一个 :
第 1 步:
为您的自定义单元格创建一个 Class。让我们说 MyCollectionViewCell
。因为您的 Cell 中有一个按钮,所以您应该在 Cell 中创建 IBAction
个按钮。
class MyCollectionViewCell: UICollectionViewCell {
@IBAction func myButtonTapped(_ sender: Any) {
}
}
第 2 步:
让我们声明一个我们将用来与 tableView/CollectionView 的数据源通信的协议。
protocol MyCollectionViewCellProtocol : NSObjectProtocol {
func buttonTapped(for cell : MyCollectionViewCell)
}
第 3 步:
让我们在 MyCollectionViewCell
中创建一个 属性weak var delegate : MyCollectionViewCellProtocol? = nil
在第 3 步之后,您的 MyCollectionViewCell class 应该看起来像
protocol MyCollectionViewCellProtocol : NSObjectProtocol {
func buttonTapped(for cell : MyCollectionViewCell)
}
class MyCollectionViewCell: UICollectionViewCell {
weak var delegate : MyCollectionViewCellProtocol? = nil
@IBAction func myButtonTapped(_ sender: Any) {
self.delegate?.buttonTapped(for: self)
}
}
第 4 步:
在您的 tableView 的 CellForRowAtIndexPath 或 CollectionView 的 sizeForItemAtIndexPath 中确认传递 ViewController 作为单元格的委托。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : MyCollectionViewCell = //deque your cell
(cell as! MyCollectionViewCell).delegate = self
}
第 5 步:
现在让你的ViewController确认协议
class YourViewController: UIViewController,MyCollectionViewCellProtocol {
第 6 步:
终于在你的 VC
中实现方法func buttonTapped(for cell: MyCollectionViewCell) {
let indexPath = self.collectionView?.indexPath(for: cell)
//access array now
}
P.S:
抱歉,虽然我知道您正在使用 TableView,但我匆忙为 CollectionView 编写了代码,但委托非常相同:)我希望您能够理解其中的逻辑