Modifying Tables

Whereas setting up a table’s contents in WatchKit is markedly different from using UITableView on iOS, modifying the content once you’ve done that initial setup is pretty similar—you just need to tell the table where to add the rows. To support what you’re going to do in the next chapter, let’s include support for adding a new run to the table once you’ve set everything up.

You’ll use the interface controller’s awakeWithContext method to receive the data. When you get a Run passed in as the context, you’ll use the table’s insertRowsAtIndexes(_:withRowType:) method to add the row. You’ll then use its rowControllerAtIndex just like when you set up the table, but this time just for the newly inserted row. Your method will look like this when you’ve finished:

 override​ ​func​ awakeWithContext(context: ​AnyObject​?) {
 super​.awakeWithContext(context)
 
 if​ ​let​ run = context ​as?​ ​Run​ {
  runs?.insert(run, atIndex: 0)
 
  runTable.insertRowsAtIndexes(​NSIndexSet​(index: 0),
  withRowType: ​"RunRow"​)
 
 if​ ​let​ rowc = runTable.rowControllerAtIndex(0) ​as?​ ​RunLogRowController​ {
  configureRow(rowc, forRun: run)
  }
  }
 }

Thanks to the configureRow(_:forRun:) method we wrote,, you need to reuse very little code here. This is a pretty simple case, since you only need to add a row to the top of the table. You create an NSIndexSet to store the indexes of rows you need to add. If you’re adding rows all over the place in your table, you may want to take advantage of NSIndexSet’s enumerateIndexesUsingBlock(_:) method to iterate over your row controllers, as follows:

 var​ indexSet: ​NSIndexSet​ ​// a complicated index set
 
 table.insertRowsAtIndexes(indexSet, withRowType:​"RowType"​)
 
 indexSet.enumerateIndexesUsingBlock { index, _ ​in
 let​ rowController = table.rowControllerAtIndex(index)
 
 // configure row here
 }

In this example, indexSet contains rows throughout the data set. You insert the rows into the table and then call enumerateIndexesUsingBlock(_:). This method runs the closure you pass into it once for each index in the index set. For each index, you’d configure a row controller.

It’s important to make sure you always get the row controller for the proper index(es), and the preceding technique can help you avoid mistakes. As with setting up content for the first time, this example was pretty easy because the rows are all of the same type. If you need to insert rows of multiple types, you’ll need to call insertRowsAtIndexes(_:withRowType:) for each row type; there’s no method to add multiple row types in one go.

Deleting rows from the table is much the same as inserting them, but you don’t need to do any work once they’re gone. Simply call removeRowsAtIndexes with an index set, and your table will automatically remove the rows and destroy the associated row controllers for you. You can call this method no matter what the row types are for the rows in question.

That’s all there is to modifying the content of the table once it’s set up. You can insert and delete rows, but unlike with UITableView on iPhone, there are no facilities to reorder rows, nor is there any swipe-to-delete functionality. The only way to get user input from a table is to have the user tap it. Let’s explore that next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.74.27