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

Custom indexTitles #27

Open
wow-such-amazing opened this issue Jun 20, 2019 · 3 comments
Open

Custom indexTitles #27

wow-such-amazing opened this issue Jun 20, 2019 · 3 comments

Comments

@wow-such-amazing
Copy link
Contributor

wow-such-amazing commented Jun 20, 2019

Hello!

I have a suggestion to improve about indexTitles for sections.
Below is the current code for section indexes. It is based on the indexTitle property of the TableSection. However pretty common case is to have the full alphabet in the indexes even if your table contains only couple sections with titles like C and H. So I think the best way would be to somehow divide indexTitles from sections.

I will be able to make a PR about that as soon as we switch our project from FlowKit to Owl.

// MARK: - Section Indexes -
public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
	let indexes = sections.compactMap({ $0.indexTitle })
	guard !indexes.isEmpty else {
		return nil
	}
	return indexes
}

public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
	return sections.firstIndex(where: { $0.indexTitle == title }) ?? 0
}
@wow-such-amazing
Copy link
Contributor Author

I think I found a way how this can work. Maybe we can document it

If I create a TableSection without models and without header, but with indexTitle then I will have index but no data in the table. And later I can update this section when I have some data 👍

@wow-such-amazing
Copy link
Contributor Author

However in this case it would be useful to mark elements as open. So that we could update header in didSet of elements. Or maybe there could be an event that is triggered whenever elements are updated

@wow-such-amazing
Copy link
Contributor Author

I think code will describe my idea in a better way. This is what I came up with for now

final class TestSection: TableSection {
	required init(original: TableSection) {
		super.init(original: original)
	}

	required init<C>(source: TableSection, elements: C) where C : Collection, C.Element == ElementRepresentable {
		super.init(source: source, elements: elements)
	}

	func apply(elements: [ElementRepresentable]) {
		removeAll()
		set(elements: elements)
		reloadHeader()
	}

	func reloadHeader() {
		guard let indexTitle = indexTitle else { return }
		if elements.isEmpty {
			headerView = nil
		} else {
			let someConfig = SomeHeaderConfig(title: indexTitle, color: .black)
			headerView = SomeHeaderAdapter(config: someConfig)
		}
	}
}

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

No branches or pull requests

1 participant