Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for custom TagView subclass and default select tag action #187

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

katerynasytnykiOS
Copy link

@katerynasytnykiOS katerynasytnykiOS commented Jun 22, 2018

Here a couple of fixes.

  1. It turned out it was not possible to subclass TagView - when implementing a subclass the tags would not show up at all, only setting tags as strings would work. It was happening because "addTags" method would call "createNewTagView" but "addTagViews" would not call that method and only append the tags.
    The reason to even do subclassing - if each tags represents a model subclassing would allow associate a model with a tag and not have to match what models were selected by a string value of selected titles. For example, below is the implementation of a subclass that works with this pull request changes - each tag represents a category in this case, and there's an extension for the TagListView that returns and sets a list of categories:
import UIKit
import TagListView

/// Custom TagView that represents a Category
class CategoryCloudView: TagView {
  var category: Category
  
  init(category: Category) {
    self.category = category
    super.init(title: category.name)
  }
  
  required init?(coder aDecoder: NSCoder) {
    self.category = Category()
    super.init(coder: aDecoder)
  }
  
  static func createCategoryCloudViews(categories: [Category]) -> [CategoryCloudView] {
    return categories.map {
      return CategoryCloudView(category: $0)
    }
  }
}

// Categories support
extension TagListView {
  var selectedCategories: [Category]? {
    guard let selectedCategoryCloudViews = selectedTags() as? [CategoryCloudView] else { return nil }
    let categories = selectedCategoryCloudViews.map {
      return $0.category
    }
    return categories
  }
  
  func addCategoryCloudViews(_ categories: [Category]) {
    let categoryCloudViews = CategoryCloudView.createCategoryCloudViews(categories: categories)
    addTagViews(categoryCloudViews)
  }
}
  1. Default support for selecting an item. Defining a delegate and setting selection there works but seems like it should be a part of default functionality and if there are no other custom actions done in the delegate method it eliminates the need to even implement the delegate.

Let me know what you think - happy to make changes!
Best,
Kateryna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants