开发者

UITableView+.plist+UINavigationController issue

开发者 https://www.devze.com 2023-04-10 18:50 出处:网络
As my first app I am trying to master simple Navigation Based numismatic collection app. There\'s some trouble with indexes in my array filled from .plist. Plist has 191 records; the ordinary list o

As my first app I am trying to master simple Navigation Based numismatic collection app.

There's some trouble with indexes in my array filled from .plist. Plist has 191 records; the ordinary list of countries -> item 0 - String - Albania ... item 190 - String - Vanuatu.

I read these records into my temporary array to populate UITableView.

NSString *countriesFile=[[NSBundle mainBundle]pathForResource:@"countries" ofType:@"plist"];
countries=[[NSArray alloc]initWithContentsOfFile:countriesFile];

This table view has sections -> continents.

UITableView+.plist+UINavigationController issue

Well, my problem is:

When pushing to another nib, it should read the name of country and show it in header and label. So it does, but only for european countries. If I tap some other continent's country, I see some european country. Please see screenshots

Tapping on european country -> Belgium开发者_运维知识库, shows right.

UITableView+.plist+UINavigationController issue

Moving to african section:

UITableView+.plist+UINavigationController issue

Taping on Angola and getting Austria.

UITableView+.plist+UINavigationController issue

So, Angola's index - 92. Austria - 1. But Austria and Angola are both second cell in their native section.

Where's the problem and what would it be?

Thanks in advance.


I am assuming you are using -(void)tableView: (UITableView*)tableView didSelectRowAtIndexPath: (NSIndexPath*)indexPath to tell when you have clicked on an item in the list? Also, are you using [indexPath row] (or indexPath.row) to figure out the index of the row you tapped on?

What the row property returns is the row index within the current section. It does NOT return the absolute row index for the entire table. Because of this, it does not matter if you click on the index two item in the first section or the index two item in the second section, row will return 2 in both of those cases.

An idea would be to change the .plist file a bit. Instead of it only having one long array of countries, have a parent array that represents the continents. Then the object for each index in this continent array, have another array that will hold the list of countries in there.

-(void)tableView: (UITableView*)tableView didSelectRowAtIndexPath: (NSIndexPath*)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row=[indexPath row];
    NSString* countryName=[[plistArray objectAtIndex: section] objectAtIndex: row];
    //rest of code...
}


It sounds like you are not taking into account the section value of indexPath. An indexPath has two parts, a section and a row. In tables that have a single section you usually just read the row value. In your case it looks like you have a grouped table so you need to read both indexPath.section and indexPath.row for the data location.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号