Reputation: 5780
In an attempt to better understand tables for future use, I was hoping somebody could give me some simple syntax on the use of this method. I'm hoping to gain some understanding and build upon it in the future.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
So, I have this set up. Say I wanted to NSLog
the contents of the row that was selected. How would I go about doing that?
Thus far, I've tried it my own way several times, however it either returns nil
or I get an error.
EDIT: Found it myself. Solution:
NSLog(@"Row: %@", [dataArray objectAtIndex:indexPath.row]);
Upvotes: 0
Views: 168
Reputation: 64012
It's impossible to answer this definitively without your actual code, but broadly:
This is a method in the UITableViewDelegate
protocol. The table view's delegate is an object that the table view essentially asks for its "opinion" about what to do when certain events happen, such as here, when the user taps on a row to select it.
Frequently, the delegate and the table view's data source are the same object. The data source, as its name suggests, provides the data which the table view displays. It does this via methods that look a lot like the table view delegate methods; the table view asks the data source for the info as necessary, rather than the data source initiating the exchange.
So, somewhere this delegate/data source object (now we're coming to the part where it would be really helpful to have your own code to talk about) has a reference to a model that represents all the info that will be displayed in the table. This could be as simple as an array, or it could be a connection to a full-on database of some kind. In any case, the table view asks the data source to provide info for each row from that model -- this happens in tableView:cellForRowAtIndexPath:
.
This method here, tableView:didSelectRowAtIndexPath:
, can also access the model, so all you have to do to inspect the contents of the row is perform the same lookup as you do in tableView:cellForRowAtIndexPath:
.
As a concrete example:
NSLog(@"%@", [modelArray objectAtIndex:[indexPath row]]);
Upvotes: 0
Reputation:
The indexPath has a category on it for dealing with tables. It responds to -row and -section.
NSLog(@"Row: %d , Section %d",[indexPath row], [indexPath section]);
Upvotes: 4