0

I have a large array of data which I am displaying in a UITable. The table is broken into sections alphabetically. When a user selects a cell, I need to figure out where in the array that item is. The only identifying data in the cell is the label, which doesn't seem to be any help. The data in the array is not necessarily ordered alphabetically either. How can I figure out which item in the array they selected. Can I tie metadata or something to the table cell? I thought to make some kind of calculation based on the indexPath and the total count in the array, but since the array is not alphabetical, and the table is, this won't work.

Hope this makes sense. Thanks for any help.

4
  • I guess what I am asking here is, how can I get the overall index of a cell, not the index path. Meaning, instead of [2, 4], I would get 16 or something. I can't calculate because I don't know the number of items in each section. Commented Aug 29, 2011 at 7:07
  • You do know the number of items in each section: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section Commented Aug 29, 2011 at 7:27
  • Ok but I would have to loop through each section? I.E. If I select Section 8, Row 2, then using indexPath.row in the array essentially turns up the second item in the array, instead of the 17th item which it actually is. Each section has a different amount of data as well. Commented Aug 29, 2011 at 14:19
  • If you want section 8, then loop through each section and aggregate the counts. When you get to section 8, add two to the count and you should come up with the right answer. Commented Aug 30, 2011 at 0:22

2 Answers 2

1

It's typically a problem that solves itself. When you build the datasource for the tableview you have to sort your array in a way that makes sense for you to be able to display the information consistently, this is also going to lead to you being able to access it consistently.

In other words you're accessing something to build the table for your datasource calls of -tableView:cellforrowatindexpath:, the algorithm you use to build the cell will be the same you'll use to access the selected cell using the index path with didSelectCellAtIndexPath:.

Sign up to request clarification or add additional context in comments.

1 Comment

agreed, I have modified the array to reflect how I am displaying in information.
0

You should look at UITableView Delegate method below:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

It will give the index path of cell that was selected. Once you have the index path you can access the row and section property like so:

indexPath.row;
indexPath.section;

Once you have these two values you can then access your array object and see what item was selected in each section:

NSLog(@"Section %i, Row %i, Item %@", indexPath.section, indexPath.row, [array objectAtIndex:indexPath.row]);

3 Comments

I have that method setup. Isn't the row relative to the section though? So Row 2 for example might be in section 4, making it the 8 element in the array. How can I make sure I am selecting the correct row from the array?
I think I need to be utilizing the sectionIndex property? But not sure how.
A tableView is a reflection of the contents in the array. When you select a cell it will return the correct item in the array, regardless of how many sections you have. Did you try implementing the NSLog statement I provided above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.