大家。现在我正在尝试创建一个UITableView,其中按字母排序的联系人姓名和个人资料图片。就像这张图片一样: 提供此图像的网站向我展示了如何制作这样的内容,但我遇到了问题,因为在我的应用程序中我们没有静态图像,但使用MySQL数据库来提供保存的联系人。我能够通过Web服务获取所需的数据并将其放在名为dataDict的NSDictionary中。在NSLog中,它显示如下:
animal={(id="1",name="BEAR",images="bear321.jpg"),(id="2",name="Dog",images="puppy.jpg"),(id="3",name="GOOSE",images="null"),...so on}
由于我不知道如何解析我所拥有的字典,所以现在我还在使用这段代码: -
(void)viewDidLoad
{
[super viewDidLoad];
animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
@"C" : @[@"Camel", @"Cockatoo"],
@"D" : @[@"Dog", @"Donkey"],
@"E" : @[@"Emu"],
@"G" : @[@"Giraffe", @"Greater Rhea"],
@"H" : @[@"Hippopotamus", @"Horse"],
@"K" : @[@"Koala"],
@"L" : @[@"Lion", @"Llama"],
@"M" : @[@"Manatus", @"Meerkat"],
@"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
@"R" : @[@"Rhinoceros"],
@"S" : @[@"Seagull"],
@"T" : @[@"Tasmania Devil"],
@"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (NSString *)getImageFilename:(NSString *)animal
{
NSString *imageFilename = [[animal lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
imageFilename = [imageFilename stringByAppendingString:@".jpg"];
return imageFilename;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [animalSectionTitles objectAtIndex:section];
NSArray *sectionAnimals = [animals objectForKey:sectionTitle];
return [sectionAnimals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
NSString *sectionTitle = [animalSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionAnimals = [animals objectForKey:sectionTitle];
NSString *animal = [sectionAnimals objectAtIndex:indexPath.row];
cell.textLabel.text = animal;
cell.imageView.image = [UIImage imageNamed:[self getImageFilename:animal]];
return cell;
}
完全相同like this site所以请帮我解决我的问题,告诉我我需要修改的内容,这样我就可以根据我的动态字典添加图片名称和图片,而不是像上面那样硬编码。非常感谢提前^^
答案 0 :(得分:0)
以下是您尝试做的一个示例。
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *animals;
@property (nonatomic, strong) NSArray *sections;
@end
@implementation ViewController
static NSString *const CellIdentifier = @"CellIdentifier";
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
NSArray *allAnimals = @[@{@"name": @"Bear"}, @{@"name": @"Black Swan"}, @{@"name": @"Buffalo"}, @{@"name": @"Camel"}, @{@"name": @"Cockatoo"}, @{@"name": @"Dog"}, @{@"name": @"Donkey"}, @{@"name": @"Emu"},
@{@"name": @"Giraffe"},@{@"name": @"Greater Rhea"}, @{@"name": @"Hippopotamus"}, @{@"name": @"Horse"}, @{@"name": @"Koala"}, @{@"name": @"Lion"}, @{@"name": @"Llama"}, @{@"name": @"Manatus"}, @{@"name": @"Meerkat"}, @{@"name": @"Panda"}, @{@"name": @"Peacock"}, @{@"name": @"Pig"}, @{@"name": @"Platypus"}, @{@"name": @"Polar Bear"}, @{@"name": @"Rhinoceros"}, @{@"name": @"Seagull"}, @{@"name": @"Tasmania Devil"}, @{@"name": @"Whale"}, @{@"name": @"Whale Shark"}, @{@"name": @"Wombat"}];
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSArray *sectionIndexTitles = [collation sectionIndexTitles];
_animals = [NSMutableArray array];
for(int i = 0 ; i < sectionIndexTitles.count; i++){
[self.animals addObject:[NSMutableArray array]];
}
for(NSDictionary *anAnimal in allAnimals){
NSString *name = anAnimal[@"name"];
NSUInteger section = [collation sectionForObject:name collationStringSelector:@selector(capitalizedString)];
NSMutableArray *array = [self.animals objectAtIndex:section];
[array addObject:anAnimal];
}
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
if([self.animals[section] count] > 0 ) return [collation sectionIndexTitles][section];
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
return [[collation sectionIndexTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *animalsInSection = [self.animals objectAtIndex:section];
return animalsInSection.count;
}
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
return [collation sectionIndexTitles];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray *animalsInSection = [self.animals objectAtIndex:indexPath.section];
cell.textLabel.text = animalsInSection[indexPath.row][@"name"];
return cell;
}
@end
我希望你正在尝试这样做。