在 Swift 的自定义单元格中显示操作 sheet
showing action sheet in the custom cell in Swift
我有一个自定义单元格,其中包含一个按钮,我想在按下按钮时显示一个动作 sheet,但如您所知,UITableViewCell 没有方法 "presentViewController",那我该怎么办?
在您的自定义单元格的 swift 文件中,编写要由您的 viewContoller 遵守的协议,
// your custom cell's swift file
protocol CustomCellDelegate {
func showActionSheet()
}
class CustomTableViewCell : UITableViewCell {
var delegate: CustomCellDelegate?
// This is the method you need to call when button is tapped.
@IBAction func buttonTapped() {
// When the button is pressed, buttonTapped method will send message to cell's delegate to call showActionSheet method.
if let delegate = self.delegate {
delegate.showActionSheet()
}
}
}
// Your tableViewController
// it should conform the protocol CustomCellDelegate
class MyTableViewController : UITableViewController, CustomCellDelegate {
// other code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellReuseIdentifier", forIndexPath: indexPath)
// configure cell
cell.delegate = self
return cell
}
// implement delegate method
func showActionSheet() {
// show action sheet
}
}
确保您的视图控制器符合 CustomCellDelegate 协议并实现 showActionSheet() 方法。
在 cellForRowAtIndexPath 数据源方法中创建单元格时,将您的 viewContoller 指定为自定义单元格的委托。
您可以通过 viewController 中的 showActionSheet 方法展示您的新视图控制器。
这就是您要执行此操作的方式:
- 为您的客户创建协议
UITableViewCell
说 MyTableViewCellDelegate
。
- 在您的协议中添加方法
cellButtonTapped
。
- 使您的视图控制器(使用这些单元格)符合
MyTableViewCellDelegate
即在头文件中添加 <MyTableViewCellDelegate>
.
- 在您的视图控制器的
cellForRowAtIndexPath:
方法中,初始化单元格时,将自己设置为委托。
- 在您的自定义 table 视图单元格 class 中,点击按钮时,将控件移交给它的委托,即您的视图控制器。
- 在您的视图控制器中实现方法
cellButtonTapped
并根据需要呈现操作 sheet。
我有一个自定义单元格,其中包含一个按钮,我想在按下按钮时显示一个动作 sheet,但如您所知,UITableViewCell 没有方法 "presentViewController",那我该怎么办?
在您的自定义单元格的 swift 文件中,编写要由您的 viewContoller 遵守的协议,
// your custom cell's swift file
protocol CustomCellDelegate {
func showActionSheet()
}
class CustomTableViewCell : UITableViewCell {
var delegate: CustomCellDelegate?
// This is the method you need to call when button is tapped.
@IBAction func buttonTapped() {
// When the button is pressed, buttonTapped method will send message to cell's delegate to call showActionSheet method.
if let delegate = self.delegate {
delegate.showActionSheet()
}
}
}
// Your tableViewController
// it should conform the protocol CustomCellDelegate
class MyTableViewController : UITableViewController, CustomCellDelegate {
// other code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellReuseIdentifier", forIndexPath: indexPath)
// configure cell
cell.delegate = self
return cell
}
// implement delegate method
func showActionSheet() {
// show action sheet
}
}
确保您的视图控制器符合 CustomCellDelegate 协议并实现 showActionSheet() 方法。
在 cellForRowAtIndexPath 数据源方法中创建单元格时,将您的 viewContoller 指定为自定义单元格的委托。
您可以通过 viewController 中的 showActionSheet 方法展示您的新视图控制器。
这就是您要执行此操作的方式:
- 为您的客户创建协议
UITableViewCell
说MyTableViewCellDelegate
。 - 在您的协议中添加方法
cellButtonTapped
。 - 使您的视图控制器(使用这些单元格)符合
MyTableViewCellDelegate
即在头文件中添加<MyTableViewCellDelegate>
. - 在您的视图控制器的
cellForRowAtIndexPath:
方法中,初始化单元格时,将自己设置为委托。 - 在您的自定义 table 视图单元格 class 中,点击按钮时,将控件移交给它的委托,即您的视图控制器。
- 在您的视图控制器中实现方法
cellButtonTapped
并根据需要呈现操作 sheet。