嗨,我正在尝试按字母顺序在地址簿部分中使用联系人填充我的表格视图,我在我的程序中使用 CFArray。到目前为止,我已经设法在部分中显示联系人,并且字母的部分计数是正确的。但问题是在每个部分中,数据都是从开头加载的,即它总是以 A 部分的名称开头,所以在 B 部分中,它也以 A 部分的名称开头,依此类推。所以只有A的部分是正确的。如何在表格视图中修改我的代码,以便它正确显示每个部分的名称,即不是从字母 A 为每个部分加载名称,而是应该在 B 的部分中加载带有 B 的名称,在 C 的部分中加载 C 等等? 下面是我实现的代码
- (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 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;