Professional Documents
Culture Documents
Suppose the data source for the table view is the array dwarves
• var cell =
tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier)
• if (cell == nil) {
• cell = UITableViewCell(style: UITableViewCellStyle.Default,
reuseIdentifier: simpleTableIdentifier)
• }
iOS makes a new table view cell only when it cannot re-use a cell, for
efficiency
Object A may specify a delegate (another
object) B
connect the table view cell with the data source
•
• cell!.textLabel?.text = dwarves[indexPath.row]
• cell!.textLabel?.font = UIFont .boldSystemFontOfSize(50)
• return cell!
var cell =
tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier)
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:
simpleTableIdentifier)
}
cell!.textLabel?.text = String(name[indexPath.row])
return cell!
}
Method to send data
// This function is called before the segue "ShowDetail" is made from this view
controller to the destination controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowDetail" {
//get the index of the row selected in the table
let indexPath = tableView.indexPathForCell(sender as! UITableViewCell)!;
• Consider:
• the TypingViewController has a Save button
• when the button is pressed
• data is sent to the ViewController
• Steps to be done
• In the ViewController, implement an IBAction to receive the data
• @IBAction func unwindToViewController(sender: UIStoryboardSegue)
{ …} // you may name the function anything
var writing:String?
self.writing = writing
resultTextField.text = self.writing
}
}
}
Code of the TypingViewController (Source)
// other methods
Import a property list into a table view
table view data source methods
class ViewController: UIViewController, UITableViewDataSource {
var names: [String: [String]]!
var keys: [String]!
return cell
}
How to make a search bar
Three steps for search bar
1.Specify the data to be searched
Description
searchResultsUpdater
weak var searchResultsUpdater:
UISearchResultsUpdating? { get set }
The object responsible for updating the contents of the search results
controller.
protocol UISearchResultsUpdating
protocol UISearchResultsUpdating : NSObjectProtocol
Description
SearchResultsController
class SearchResultsController: UITableViewController, UISearchResultsUpdating {
private static let longNameSize = 6
private static let shortNamesButtonIndex = 1
private static let longNamesButtonIndex = 2
let sectionsTableIdentifier = “SectionsTableIdentifier"
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: sectionsTableIdentifier)
} ..
SearchResultsController 2
class SearchResultsController: UITableViewController, UISearchResultsUpdating {
//…
func updateSearchResultsForSearchController(searchController:
UISearchController)
Description
Called when the search bar becomes the first responder or when the user makes
changes inside the search bar.
This method is automatically called whenever the search bar becomes the first
responder or changes are made to the text in the search bar. Perform any required
filtering and updating inside of this method.
SearchResultsController 3
class SearchResultsController: UITableViewController, UISearchResultsUpdating {
//…
if !searchString.isEmpty {
let isAMatch: String -> Bool = { name in // this is a closure
// Filter out long or short names depending on which
// scope button is selected.
let nameLength = name.characters.count
if (buttonIndex == SearchResultsController.shortNamesButtonIndex
&& nameLength >= SearchResultsController.longNameSize)
|| (buttonIndex == SearchResultsController.longNamesButtonIndex
&& nameLength < SearchResultsController.longNameSize) {
return false
}
tableView.reloadData()
}
}
Return an Array containing the elements of self, in order, that satisfy the predicate includeElement.