Ben's Series

Transcript

Tap on time to skip ahead

00:00

00:00

Intro

In our previous video, we explored organizing our heroes and villains in a grouped UITableView list. In this video, we’re going to look at how we can handle the tapping of row items inside UITableView. As you can see right now, as I tap on each individual row, nothing really happens. So let’s take a look at what we can do.

00:34

00:34

Override RowSelected

So I’m gonna go into my DemoTableSource class here. And we’ll see that we have our GetCell, NumberOfSections, and other various methods that we overrode in our previous video. And I’m going to go ahead and I’m gonna override one more method and it’s going to be called RowSelected. And much like the GetCell method, we will see that we have a table view that’s passed to it, as well as a IndexPath. And in this particular case the IndexPath will represent the particular row number that was tapped.

01:13

01:13

Displaying an Alert with RowSelected

So the first thing I wanna do is get the key for the particular section. So I’m going to say “var key= this.Characters.Keys[indexPath.Section]” And this section represents the header or group of items. And then we’ll also see here that we have this compile error here and because we need to convert Keys ToList. And that should make it go away. And then to get the actual hero or villain that was tapped I can say “var val = this.Characters[key][indexPath.Row]” And so now let’s do something to let the user know that they tapped. So I’m going to say: “new UIAlertView(“Item Tapped”, string.Format(“{0} was tapped “, val), null, null, null).Show()”

02:50

02:50

Deselecting Tapped Row

So now if I run our application. When I tap on any these items, we now get a message here. Might be nice if we had an okay button. So I’m going to set the cancel button title to “Ok.” Now when I tap this it will say “Item Tapped – Superman was tapped” And if I tap here, it will say “Penguin was tapped.” Now one of the things you will notice is that whenever I tap we still have this selected state for the table view item. To kind of get rid of that, we can just say “tableView.DeselectRow(indexPath, true)” And the true says that we want to deselection to be animated. And now you see it no longer stays highlighted.

03:52

03:52

Handling RowSelected from the ViewController

And then the last thing that you can do is – right now all this stuff is happening inside the UITableViewSource. But what if our ViewController wants to do something based on thetable being tapped? So what I can do is create an Action that our ViewController can listen to. And we’re going to change this down here. And now I can move this UIAlertView stuff up here. And I want to go ahead and make a local reference to our TableSource. And now I can say “source.ItemTapped += (obj) => {..}”. Now when we run this – our ViewController will handle the tap instead our UITableViewSource. So now when I tap our view controller. And this might come in handy if you want to navigate to another view controller or whatnot. So you can see it’s real easy with UITableView to handle row taps.