Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView, how do I know what Section during cellForRowAtIndexPath?

I have a UITableView displaying a list of Cities. I want to separate them by State. I can't seem to figure out how to get it to pick the right items out of my Array. If Section 1 (Arizona) has 2 Cities and Section 2 (California) has 2 Cities, during cellForRowAtIndexPath, Section 2, City 1 has an index of 0, even though it's the 3rd item in my array. I thought about just turning my City Array into a State Array, where each item holds an Array of Cities, but I still don't know what section I'm on and therefore don't know which City Array under the States Array I would need to access.

like image 306
James P. Wright Avatar asked Nov 04 '09 20:11

James P. Wright


People also ask

What is Indexpath section?

An index number identifying a row in a UITableView object in a section identified by section. section.

What does cellForRowAt do?

Returns the table cell at the index path you specify.


2 Answers

The method is called

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

The indexpath parameter contains a row and section property.

indexPath.section   indexPath.row 

Here's documentation link for the NSIndexPath class.

like image 117
ynnckcmprnl Avatar answered Oct 17 '22 22:10

ynnckcmprnl


you can use indexPath.section and indexPath.row

like image 39
Morion Avatar answered Oct 18 '22 00:10

Morion