Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在ViewController中为TableView继承自定义TableViewController_Ios_Swift - Fatal编程技术网

Ios 如何在ViewController中为TableView继承自定义TableViewController

Ios 如何在ViewController中为TableView继承自定义TableViewController,ios,swift,Ios,Swift,因此,我有一个自定义SwipeCellTableView类,在使用UITableViewController时继承了该类。现在我只想在常规视图控制器中为ib outlet表视图控制器使用该类。事实证明,这是非常困难的,似乎不再值得。这能做到吗 这是从TableViewController继承的超类,我曾尝试将其更改为从视图控制器继承,但没有成功 class SwipeTableViewController: UITableViewController, SwipeTableViewCellDel

因此,我有一个自定义SwipeCellTableView类,在使用UITableViewController时继承了该类。现在我只想在常规视图控制器中为ib outlet表视图控制器使用该类。事实证明,这是非常困难的,似乎不再值得。这能做到吗

这是从TableViewController继承的超类,我曾尝试将其更改为从视图控制器继承,但没有成功

class SwipeTableViewController: UITableViewController, SwipeTableViewCellDelegate {

var cell: UITableViewCell?

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = 80.0
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SwipeTableViewCell

    cell.delegate = self
    return cell
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
    guard orientation == .right else { return nil }

    let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
        // handle action by updating model with deletion
        self.updateModel(at: indexPath)
    }

    deleteAction.image = UIImage(named: "delete-icon")

    return [deleteAction]
}

func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
    var options = SwipeTableOptions()
    options.expansionStyle = .destructive
    //options.transitionStyle = .reveal
    return options
}

func updateModel(at indexPath: IndexPath){
    //update data model
    print("Item deleted from super class")
}
这是我试图从中访问的视图控制器:

class GoalsViewController: UIViewController, SwipeTableViewController {

@IBOutlet weak var categoryTable: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

@IBAction func addCategoryPressed(_ sender: UIButton) {
    performSegue(withIdentifier: "showgoalsSeg", sender: self)
}
有关我以前在使用实际TableViewController时如何使用它的参考信息:

class CategoryViewController: SwipeTableViewController {

var categories: Results<Category>? //optional so we can be safe

override func viewDidLoad() {
    super.viewDidLoad()
    loadCategory()
    tableView.rowHeight = 80.0
    tableView.separatorStyle = .none
}
//MARK: - Tableview Datasource Methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //Only get the count of categories if it's nil, else 1
    return categories?.count ?? 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //fetching cell from super view
    let cell = super.tableView(tableView, cellForRowAt: indexPath)

    cell.textLabel?.text = categories?[indexPath.row].name ?? "No Categories Added Yet"



    cell.backgroundColor = UIColor(hexString: categories?[indexPath.row].color ?? "000000")

    return cell
}


//MARK: - Tableview Delegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "goToItems", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! ToDoListViewController

    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.selectedCategory = categories?[indexPath.row]
    }
}
//MARK: - Add New Categories

@IBAction func addButtonPressed(_ sender: Any) {
    var textField = UITextField()

    let alert = UIAlertController(title: "Add New Category", message: "", preferredStyle: .alert)

    let action = UIAlertAction(title: "Add Category", style: .default) { (action) in

        let newCategory = Category()
        newCategory.name = textField.text!
        newCategory.color = UIColor.randomFlat.hexValue()

        self.save(category: newCategory)
    }
    alert.addAction(action)

    alert.addTextField { (field) in
        textField = field
        textField.placeholder = "Add a new category"
    }
    present(alert, animated: true, completion: nil)
}

func save(category: Category){
    let realm = try! Realm()
    do {
        try realm.write{
            realm.add(category)
        }
    } catch {
        print("error saving context")
    }

    tableView.reloadData()
}

override func updateModel(at indexPath: IndexPath) {
    super.updateModel(at: indexPath)
    let realm = try! Realm()
    if let categoryForDeletion = self.categories?[indexPath.row]{
        do{
            try realm.write{
                realm.delete(categoryForDeletion)
            }
        } catch {
            print("error deleting cell")
        }
        //tableView.reloadData()
    }

}

func loadCategory(){
    let realm = try! Realm()
    categories = realm.objects(Category.self)
    tableView.reloadData()
}
class CategoryViewController:SwipeTableViewController{
变量类别:结果?//可选,这样我们就安全了
重写func viewDidLoad(){
super.viewDidLoad()
loadCategory()
tableView.rowHeight=80.0
tableView.separatorStyle=.none
}
//MARK:-Tableview数据源方法
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//只有当类别数为零时,才能得到类别数,否则为1
退货类别?计数??1
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
//从超级视图获取单元格
让cell=super.tableView(tableView,cellForRowAt:indexPath)
cell.textlab?.text=categories?[indexath.row].name???“尚未添加类别”
cell.backgroundColor=UIColor(hexString:categories?[indexath.row].color???“000000”)
返回单元
}
//MARK:-Tableview委托方法
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
性能检查(带有标识符:“goToItems”,发送者:self)
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
让destinationVC=segue.destination为!ToDoListViewController
如果让indexPath=tableView.indexPathForSelectedRow{
destinationVC.selectedCategory=类别?[indexath.row]
}
}
//标记:-添加新类别
@iAction func AddButton已按下(\发送方:任意){
var textField=UITextField()
let alert=UIAlertController(标题:“添加新类别”,消息:,首选样式:。警报)
让action=UIAlertAction(标题:“添加类别”,样式:。默认值){(操作)在
设newCategory=Category()
newCategory.name=textField.text!
newCategory.color=UIColor.randomFlat.hexValue()
self.save(类别:newCategory)
}
alert.addAction(操作)
alert.addTextField{(字段)位于
text字段=字段
textField.placeholder=“添加新类别”
}
当前(警报、动画:真、完成:无)
}
功能保存(类别:类别){
让realm=try!realm()
做{
试试realm.write{
realm.add(类别)
}
}抓住{
打印(“保存上下文时出错”)
}
tableView.reloadData()
}
重写func updateModel(在indexath:indexPath处){
super.updateModel(位于:indexPath)
让realm=try!realm()
如果让categoryForDeletion=self.categories?[indexPath.row]{
做{
试试realm.write{
realm.delete(CategoryForDelete)
}
}抓住{
打印(“删除单元格时出错”)
}
//tableView.reloadData()
}
}
func loadCategory(){
让realm=try!realm()
categories=realm.objects(Category.self)
tableView.reloadData()
}

这是否值得研究?还是可行?

您可以将outlet tableview委托和数据源设置为的实例SwipeTableViewController@SPatel如何从自定义类重写或访问这些类?