1

In my table view i need to display Contact name, Company name and Picture of contact person.

enter image description here

I can add/edit a contact from the Add/Edit Contact page.

enter image description here

When I entered values in add/edit page I store them in a dictionay.

I retrieved ContactNames in an array ContactsArray and using this array I tried to set sections and headers.

in this process other fields are not displayed what we have saved, observe detail Text label even this problem in didSelectRow method also..

When I selected 1st row of any section it navigates to the details page for 1sr row of 1st section

When I selected 2nd row of any section it navigates to the details page for 2nd row of 1st section

it may be the problem of sections

//Inside ViewDidLoad Method

  sections = [[NSMutableDictionary alloc] init]; ///Global Object

  BOOL found;

  for (NSString *temp in contactsArray)
  {        
        NSString *c = [temp substringToIndex:1];

        found = NO;

        for (NSString *str in [sections allKeys])
        {
              if ([str isEqualToString:c])
              {
                    found = YES;
              }
        }

        if (!found)
        {     
              [sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
  }
  for (NSString *temp in contactsArray)
  {
        [[sections objectForKey:[temp substringToIndex:1]] addObject:temp];
  }

      [contactsTable reloadData];

}

Tableview methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
      return [[sections allKeys]count];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString* CellIdentifier = @"Cell";
      UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if(cell == Nil)
      {
            cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
      }
      NSString *titleText = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

       cell.textLabel.text = titleText;

      Contact *contact = [storedContactsArray objectAtIndex:indexPath.row];
      Contact *contactPicture = [storedPicsArray objectAtIndex:indexPath.row];


          _____________________________________

           cell.detailTextLabel.text = contact.companyName;
          UIImage *contactImage =  [[UIImage alloc]initWithContentsOfFile:contactPicture.contactImage];
           cell.imageView.image = contactImage;
            _______________________________
              return cell;
}

Even when I sect Bb it navigates to hte AAAAA page details..

I tried with this link http://www.devx.com/wireless/Article/43374/0/page/2 alsoo even I repeats the same issue

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      EditContact *editContact = [[EditContact alloc] init];
      if([self.storedContactsArray count] != 0)
      {
            Contact *contactsDict = [storedContactsArray objectAtIndex:indexPath.row];
            editContact.contactInfo = contactsDict;

            Contact *picsDict = [storedPicsArray objectAtIndex:indexPath.row];
            editContact.storedPicsDict = picsDict;
      }

      [self.navigationController pushViewController:editContact animated:YES];
      [editContact release];
      [storedContactsArray release];

}

enter image description here

4

2 回答 2

0

我们可以知道您在 tableView:didSelectRowAtIndexPath: 委托方法中在做什么吗?

于 2012-11-01T07:26:21.627 回答
0

您不能在多个部分上使用行索引。在每个部分indexPath.row将再次从 0 开始。你也有你用indexPath.section

在我看来,你有两种方法可以解决这个问题:

  1. 重构你的整个代码以获得一个包含所有起始字符的字典一个包含所有联系人的数组
  2. 将每个字符(a、b 等)的整个联系人保存在sections字典的子数组中。如果你已经这样做了,你可以使用代替and ,例如像这样:sectionsstoredContactsArraystoredPicsArrayNSDictionary *contactData = [[[sections objectForKey:[[sections allKeys] objectAtIndex:indexPath.section]] objectForKey:@"contacts"] objectAtIndex:indexPath.row];

此外,您可以保存多行代码:

  for (NSString *temp in contactsArray)
  {        
        NSString *c = [temp substringToIndex:1];

        if ([sections objectForKey:c] == nil)
        {     
              [sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
  }

是相同的:

  for (NSString *temp in contactsArray)
  {        
        NSString *c = [temp substringToIndex:1];

        found = NO;

        for (NSString *str in [sections allKeys])
        {
              if ([str isEqualToString:c])
              {
                    found = YES;
              }
        }

        if (!found)
        {     
              [sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
  }
于 2012-11-01T07:44:10.617 回答