Question

Hi, I am trying to populate my tableview with contacts in the Address book section wise alphabetically, I am using CFArray in my program. So Far I've managed to display the contacts in sections and the section counts for the alphabets are correct. But the problem is in each section the data is loading from the beggining, i.e it always start with names of A's section so in B's section also it start with names with A'section and so on. So only A's section is correct. How do I modify my code in the tableview so that it displays the names correctly for each section, ie instead of loading the names from Letter A for each section it should load the Names with B in B's Section, C in C's Section and so on? Below is my code which I've implemented

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.friendsTable setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile_bg.png"]]];

    self.friendListSection = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];

}


//Reload function;Address book for changed data
-(void)viewWillAppear:(BOOL)animated
{

    [self reloadAddressBook];
    [friendsTable reloadData];
}
-(void)reloadAddressBook
{

    if(addressBook)
        CFBridgingRelease(addressBook);
    addressBook =ABAddressBookCreate();
    self.persons=ABAddressBookCopyDefaultSource(addressBook);
    contactAdd=ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook,self.persons,0);
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    //Search with Last name

    int contactCount=0, j=0;
    NSString *tweet;
    self.filteredData= CFArrayCreateMutable(kCFAllocatorDefault, 0,&kCFTypeArrayCallBacks);
    for(int i=0;i<CFArrayGetCount(self.contactAdd);i++)
    {
        self.persons=CFArrayGetValueAtIndex(self.contactAdd,i);
        tweet=[[NSString stringWithFormat:@"%@", (__bridge_transfer NSString *)ABRecordCopyValue(self.persons,kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
         NSRange contactRange= [[tweet substringToIndex:1] rangeOfString: [self.friendListSection objectAtIndex:section] options:NSCaseInsensitiveSearch];

        if(contactRange.location!=NSNotFound)
        {

            contactCount++;

        }

    }
        return contactCount;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 26;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
    return index;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.friendListSection objectAtIndex:section];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"SimpleTableCell";
    AddFriendCellView *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
                   cell= [[AddFriendCellView alloc]initWithdelegate:self];
    }



    self.persons = CFArrayGetValueAtIndex(self.contactAdd, indexPath.row);
    NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(self.persons)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [cell.userNameLabel setText:tweet];

    return cell;
}

@end

//Property Definitions in my interface file

@property AddFriendCellView *pop;
@property (weak, nonatomic) IBOutlet UITableView *friendsTable;
@property (retain, nonatomic) NSArray *friendListSection;
@property  CFArrayRef contactAdd;
@property  CFMutableArrayRef  filteredData;
@property ABRecordRef persons;
@property ABAddressBookRef addressBook;
@property (retain,nonatomic) ABPersonViewController *currentPersonViews;
Was it helpful?

Solution

I was able to figure out the that the issue was with indexPath.row so I managed a way to make it global and put this code inside my tableview and it worked fine.

NSUInteger row = 0;
NSUInteger sect = indexPath.section;
for (NSUInteger i = 0; i < sect; ++ i)
    row += [self tableView:tableView numberOfRowsInSection:i];
row += indexPath.row;
self.persons = CFArrayGetValueAtIndex(self.contactAdd, row);

OTHER TIPS

In tableview:cellForRowAtIndexPath, you are using the row - but you should also consider the section. The indexPath gives you both the row and section, and you're ignoring the section.

Specifically, the problem occurs where you say:

self.persons = CFArrayGetValueAtIndex(self.contactAdd, indexPath.row);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top