cs193p – Lecture #10 Table View

von Leutemann or Offterdinger, photo by Harke [Public domain], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

Though the lecture is called table view it starts with a “bonus topic”:

UITextField

The keyboard appears when a text field becomes first responder (becomeFirstResponder) and desperadoes when it resigns to be first responder (resignFirstResponder). To know when the return key has been pressed (functextFieldShouldReturn) or when editing has ended (textFieldDidEndEditing) is controlled via delegates or target/action notifications.

Various properties exist to control the appearance of the keyboard:

var autocapitalizationType: UITextAutocapitalizationType 
var autocorrectionType: UITextAutocorrectionType 
var returnKeyType: UIReturnKeyType 
var secureTextEntry: BOOL 
var keyboardType: UIKeyboardType 

The keyboard also provides various notifications about its current state or about the state it is going to enter (UIKeyboard{Will,Did}{Show,Hide}Notifications).

Further properties to manipulate the textview are:

var clearsOnBeginEditing: Bool
var adjustsFontSizeToFitWidth: Bool
var minimumFontSize: CGFloat 
var placeholder: String 
var background/disabledBackground: UIImage
var defaultTextAttributes: Dictionary 
var inputAccessoryView: UIView

The main part of the lecture addresses table view:

UITableView

Like text views the look and feel of a table view is controlled via delegate. Its data comes from a data-source protocol. Table-view controllers set themselves automatically as delegate and data source. If a table is dynamic the data source has to provide the number of sections (numberOfSectionsInTableView), the number of rows of each section (numberOfRowsInSection) and the views for each cell (cellForRowAtIndexPath).

A table-view cell can be the source of a segue. When preparing a segue the current cell is the sender, its index can be reference via the table-view’s indexPathForCell method.

The most important delegate methods for a table view are the ones to identify selected rows (didSelectRowAtIndexPath), or touched disclosure buttons (accessoryButtonTappedForRowWithIndexPath), or when rows will or have been selected or deselected, when the are moved, copied, deleted …

To synchronize a table with changed data we can reload the complete table (reloadData) or parts of it (reloadRowsAtIndexPaths).

Since iOS 8 autolayout can adjust the hight of the cells automatically (UITableViewAutomaticDimension/estimatedRowHeight), or the can be set the “old” way manually via heightForRowAtIndexPath.

The last part of the lecture provides a demo on the just learned. The provided twitter library contains a minor glitch. The code assumes to run on a device/simulator using the US locale. If another incompatible locale is used the code fails to recognize the date format returned by twitter. A work around is to force the data formatter to use the US locale by default:

dateFormatter.locale = NSLocale(localeIdentifier: "en_US")

The lecture and its slides are available via iTunes named “10. Table View”. The code for Smashtag is available on GitHub and at Stanford.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

8 thoughts on “cs193p – Lecture #10 Table View”

  1. In the smashtag demo used in lecture. I was not able to figure out how to make the keyboard disappear after input. My understanding was that the code below should make that happen it did not work for me when I run the code in simulator or my iphone 6. Any ideas?

    func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == searchTextField {
    textField.resignFirstResponder()
    searchText = textField.text
    }
    return true
    }

    1. textFieldShouldReturn fires when you press the return key – the problem is finding the return key … it’s easy in the simulator, just press enter on your physical keyboard (it might be you disabled it, have a look of the hardware settings) … or you change to the numbers view of the twitter keyboard, there you will find a return key …

      Without pressing a return key, the text view has no way of knowing that you have finished … and therefore the mentioned method does not get called, the first responder does not resign and the keyboard stays open …

      … you could use another keyboard, or a custom one, or you could just add a return key to a standard keyboard, or you could add a touch gesture outside to keyboard to close it …

  2. Hi Martin, I’m stuck, with something simple but I need help.
    I recently create a Twitter Account to follow the Demo. I installed the Twitter Application and additional I added the account to the Internet Accounts in the System Preferences. When I run the first part of the demo, I receive the following error.
    “TwitterRequest: Couldn\’t discover Twitter account type.”
    I track the error, and the verification below returns always nil.
    let account = accountStore.accountsWithAccountType(twitterAccountType)?.last as? ACAccount

    There is something that I will need to setup before? I work in moment without a developer account, I need it for this task?. Maybe the problem is simple to solve, but I don’t find it.

    1. Sorry, I’m very new, I did not realize that the Simulator is a complete simulation of an iphone/ipad device. Then, I went to Settings in the Simulated iphone and add my Twitter Account. And everything works perfect. Additional I saw that my devices were set in US. Therefore I did not had problems with the International Settings, I will verify now my application with the Spanish and European Settings.

      1. Thank you for posting Christian. You are a life saver. I got stuck with the same problem and didn’t find the solution until I read your post!

Leave a Reply

Your email address will not be published. Required fields are marked *