Обновлено до Swift 4.2 и добавлены изображения.Перейдите по ссылке ниже для получения дополнительной информации.
Как настроить UIAlertController с UITableView или есть какой-либо элемент управления по умолчанию, такой как этот пользовательский интерфейс в приложении Pages?
import Foundation
import UIKit
class CustomActionSheet: UIAlertController{
private var controller : UITableViewController
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
controller = UITableViewController(style: .plain)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
controller.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
controller.tableView.dataSource = self
controller.tableView.addObserver(self, forKeyPath: "contentSize", options: [.initial, .new], context: nil)
self.setValue(controller, forKey: "contentViewController")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
controller.tableView.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard keyPath == "contentSize" else {
return
}
controller.preferredContentSize = controller.tableView.contentSize
}
}
extension CustomActionSheet: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
switch(indexPath.row) {
case 0:
cell.textLabel?.text = "Share Link via iCloud"
cell.accessoryView = UIImageView(image:UIImage(named:"image1.png")!)
break
case 1:
cell.textLabel?.text = "Send a Copy"
cell.accessoryView = UIImageView(image:UIImage(named:"image2.png")!)
break
case 2:
cell.textLabel?.text = "Open in Another App"
cell.accessoryView = UIImageView(image:UIImage(named:"image3.png")!)
break
case 3:
cell.textLabel?.text = "Move to..."
cell.accessoryView = UIImageView(image:UIImage(named:"image4.png")!)
break
default:
fatalError()
}
return cell
}
}
Вы можете использовать этот пользовательский класс из своего представленияконтроллер
let controller = CustomActionSheet(title: nil, message: nil, preferredStyle: .actionSheet)
controller.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(controller, animated: true, completion: nil)