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

时间:2018-06-23 01:40:19

标签: ios swift

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

这里是从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()
}

这甚至值得说服吗?还是可行的?

0 个答案:

没有答案