Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView and alphabetical order
    primarykey
    data
    text
    <p>In my app I parse an XML file then I would like to show the entry of this file in a UITableView. I found on the <a href="http://www.devx.com/wireless/Article/43374" rel="nofollow noreferrer">web</a> how to make section in alphabetical order (like iPhone contacts), it works fine with my app. When i tap on a row in my tableview I want to display another ViewController, in which I will find some information about the row I tapped, but I'm having some problem: when I tap on a row the variable <code>indexPath.row</code> refers to the section and the information in new view controller aren't right. I will post here some screenshot to show you want I'm trying to explain.</p> <p>In the following pictures you can see how the app should work:</p> <p><img src="https://i.stack.imgur.com/9ov9w.png" alt="Correct"></p> <p>In the following pictures you can see the error of my app:</p> <p><img src="https://i.stack.imgur.com/guQwd.png" alt="error"></p> <p>You can see that in picture 1 the name is the same and in picture 2 you can see that the name it's wrong. I guess it depend on the variable <code>indexPath.row</code>. I will post here the code to create and populate the tableview:</p> <pre><code>#import "TableWasteViewController.h" #import "WasteXmlParser.h" #import "WasteDetailViewController.h" @interface TableWasteViewController () @property(nonatomic,strong)NSArray *arrayWastes; @property(nonatomic,strong)NSMutableArray *typeOfWaste; @property(nonatomic,strong)NSMutableArray *typeOfBin; @property(nonatomic,strong)NSMutableArray *indexWastes; @property(nonatomic,strong)NSMutableArray *typeOfWasteBackup; @end @implementation TableWasteViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. WasteXmlParser *parser = [[WasteXmlParser alloc]init]; [parser parseWasteXml]; self.arrayWastes = [[NSArray alloc]init]; self.arrayWastes = [parser.arrayWastes mutableCopy]; self.indexWastes = [[NSMutableArray alloc]init]; self.typeOfWaste = [[NSMutableArray alloc]init]; self.typeOfBin = [[NSMutableArray alloc]init]; for (int i = 0; i &lt; [self.arrayWastes count]; i++) { [self.typeOfWaste addObject:[[self.arrayWastes objectAtIndex:i] objectForKey:@"type"]]; [self.typeOfBin addObject:[[self.arrayWastes objectAtIndex:i]objectForKey:@"place"]]; } for (int i = 0; i &lt; [self.typeOfWaste count]-1; i++) { char alphabet = [[self.typeOfWaste objectAtIndex:i] characterAtIndex:0]; NSString *uniChar = [NSString stringWithFormat:@"%c", alphabet]; if (![self.indexWastes containsObject:uniChar]) { [self.indexWastes addObject:uniChar]; } } self.typeOfWasteBackup = [self.typeOfWaste mutableCopy]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.indexWastes count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [self.indexWastes objectAtIndex:section]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *alphabet = [self.indexWastes objectAtIndex:section]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",alphabet]; NSArray *wastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate]; return [wastes count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; UIFont *myFont = [UIFont fontWithName:@"Arial" size:14.0]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } NSString *alphabet = [self.indexWastes objectAtIndex:[indexPath section]]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; NSArray *wastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate]; if ([wastes count] &gt; 0) { NSString *cellValue = [wastes objectAtIndex:indexPath.row]; cell.textLabel.font = myFont; cell.textLabel.numberOfLines = 2; cell.textLabel.text = cellValue; } return cell; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.indexWastes; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSIndexPath *indexPath = [self.tableWaste indexPathForSelectedRow]; WasteDetailViewController *vc = segue.destinationViewController; vc.typeOfWaste = [self.typeOfWaste objectAtIndex:indexPath.row]; vc.typeOfBin = [self.typeOfBin objectAtIndex:indexPath.row]; vc.urlPic = [self.arrayWastes[indexPath.row]objectForKey:@"imgUrl"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)backToHome:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } @end </code></pre> <p>I hope you can help me to fix this issue. Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload