UITableView Sections Filtering
Intermediate Training Core Data
Many applications that you use on your iOS devices will contain lists that are grouped in certain sections. For example you can think of your address book where all your contacts are grouped under letters that their last names start with. In today's lesson, we look at how UITableViews can be sectioned off for employees with short names and long names. In doing so, we learn how we can apply a filter function on all employees to easily get the employees we want. Finally, the lesson ends in providing a much more elegant solution with a two-dimensional array to support our sections.

Comments (11)
tonyl
5 years ago
Great Lesson! I like how you first show us the long way then you show us the correct way
Kilian Hiestermann
5 years ago
I really like that too. It improves the learning curve.
mohitnandwani
5 years ago
Hey Brian! How is this ternary operator for label text: label.text = section == 0 ? "Short Names" : (section == 1 ? "Long Names" : "Really Long Names")
Brian Voong
5 years ago
jesseIOS17
5 years ago
Probably addressed in a later episode, but currently the 'new employee' functionality doesn't update the tableview until you back-out of the EmployeeController and reload.
Brian Voong
5 years ago
dclawson
5 years ago
Glad I saw this. I was worried that I'd broken something along the way!
Vakhtangi Beridze
5 years ago
Hi Brian, you missed case when employee name is equal to 6 it never prints ;)
Gabriel
5 years ago
Can i ask why we don't use NSFetchedResultsController?
Brian Voong
5 years ago
William Muñoz Rodas
5 years ago
Hi, Brian be nice with the employees who their name are 6 characters long... Very good explanation of how to create a tableView sections. Thank you.
Juanpablo Macias
4 years ago
Swift 4.2 code: override func drawText(in rect: CGRect) { let insets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0) let customRect = rect.inset(by: insets) super.drawText(in: customRect) }
LandsPete
4 years ago
Hi there Juanpablo Macias, good to now this lines, thanks a lot!
Cinquain
4 years ago
thanks!
Beetroot
4 years ago
Thanks!!!
werg09
4 years ago
leaving a comment only because i felt obliged to do so.... Great tutorial
G123
4 years ago
Hey Brian, How would I add another label to the right side of each section header that displays the birthday textfield date string of the employee. I switched the date to string so it sends "0's" (numbers) and not "mm/dd/yyyy" through. I am trying to add up the number of employees so each employee has a # and not birthdate. Thank you and thanks again for the tutorial.
Brian Voong
4 years ago
G123
4 years ago
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) let employee = allEmployees[indexPath.section][indexPath.row] cell.textLabel?.text = employee.name if let birthday = employee.employeeInformation?.birthday { cell.textLabel?.text = "\(employee.name ?? "") \(birthday)" LabelText = birthday } cell.backgroundColor = UIColor.white cell.textLabel?.textColor = .lightGray cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 17) return cell } override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let lebel = LabelText let label = IndentedLabel() if section == 0 { label.text = " \("Total") \(lebel)" //label.layer.cornerRadius = 8 label.layer.borderWidth = 10 label.backgroundColor = UIColor.tealColor } else if section == 1 { label.text = EmployeeType.Intern.rawValue label.layer.borderWidth = 4 label.backgroundColor = UIColor.lightRed } else if section == 2 { label.text = EmployeeType.Executive.rawValue label.layer.borderWidth = 4 label.backgroundColor = UIColor.lightRed } else if section == 3 { label.text = EmployeeType.SeniorManagement.rawValue label.layer.borderWidth = 4 label.backgroundColor = UIColor.lightRed } else if section == 4 { label.text = EmployeeType.Staff.rawValue label.layer.borderWidth = 4 label.backgroundColor = UIColor.lightRed } label.textColor = UIColor.white label.font = UIFont.boldSystemFont(ofSize: 24) label.layer.borderColor = UIColor.white.cgColor return label }
G123
4 years ago
Thats my code for EmployeesController. I can display a label in the section 0 that displays the last birthday text sent through, but how can I add the birthday text together instead of just displaying the birthday text of the last employee created. Thank you! Also, is there anyway to email you directly? I have some questions about the other tutorials before I purchase it.Thanks again
Brian Voong
4 years ago
Sa Shankar
4 years ago
Hi Brian I'm getting error while creating indetLabel code at UIEdgeInsetsInsetRect class. can you help me. class IndentLabel: UILabel { override func drawText(in rect: CGRect) { let insert = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0) let customRect = UIEdgeInsetsInsetRect(rect, insert) // error super.drawText(in: customRect) } } Thanks Sa Sha
bye
4 years ago
UIEdgeInsetsInsetRect has been replaced, change your code to this: class IndentedLabel: UILabel { override func drawText(in rect: CGRect) { let insets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0) let customRect = rect.inset(by: insets) super.drawText(in: customRect) } }
Cinquain
4 years ago
Ugh... this video was so beautiful! Rewatch for sure
HELP & SUPPORT