Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从MySQL创建带有排序名称和图像的联系人列表_Ios_Objective C_Uitableview_Nsdictionary - Fatal编程技术网

Ios 从MySQL创建带有排序名称和图像的联系人列表

Ios 从MySQL创建带有排序名称和图像的联系人列表,ios,objective-c,uitableview,nsdictionary,Ios,Objective C,Uitableview,Nsdictionary,大家好。现在我正在尝试制作一个UITableView,它按字母顺序排列联系人姓名和个人资料图片。如图所示: 提供此图像的网站向我展示了如何制作类似的内容,但我遇到了问题,因为在我的应用程序中,我们没有静态图像,而是使用MySQL数据库来提供保存的联系人。我能够通过web服务获取所需的数据,并将其放入名为dataDict的NSDictionary中。在NSLog中,它显示如下: animal={(id="1",name="BEAR",images="bear321.jpg"),(id="2",na

大家好。现在我正在尝试制作一个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;
}

完全相同,所以请告诉我需要修改什么来帮助我解决问题,这样我就可以根据我的动态字典添加图片名称和图像,而不是像上面那样硬编码。提前非常感谢^^

以下是您尝试做的一个示例

@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
@界面视图控制器()
@属性(非原子,强)NSMutableArray*动物;
@属性(非原子、强)NSArray*部分;
@结束
@实现视图控制器
静态NSString*常量CellIdentifier=@“CellIdentifier”;
-(无效)viewDidLoad{
[超级视图下载];
[self.tableView注册表类:[UITableViewCell类]forCellReuseIdentifier:CellIdentifier];
NSArray*allAnimals=@[{@“姓名”:“熊”}、{@“姓名”:“黑天鹅”}、{@“姓名”:“水牛”}、{@“姓名”:“骆驼”}、{@“姓名”:“凤头鹦鹉”}、{@“姓名”:“狗”}、{@“姓名”:“驴”}、{@“姓名”:“鸸鹋”},
@“名称”名称名称“““名称”名称“@“名称”名称“@“名称”名称““@“名称”名称“@“名称”名称“@“名称”名称“@“名称”名称“@“名称”名称“@“名名名名名名”“““长长颈鹿”的,”,”,”,”,”,”名“名称“@“名称”的“名称““““@“名”的“长长颈颈颈鹿”的,”,”,”,”,”,”,”,”名名名名名名““““““““““@“名称““““@“名称“““@“名称”的,”,”,”结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结结,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”"名称:"鸭嘴兽","名称":"北极熊","名称":@"犀牛","名字":"海鸥","名字":"塔斯马尼亚魔鬼","名字":"鲸","名字:"袋熊",;
UILocalizedIndexedCollation*排序=[UILocalizedIndexedCollation currentCollation];
NSArray*sectionIndexTitles=[排序规则sectionIndexTitles];
_动物=[NSMutableArray];
对于(int i=0;i0)返回[collation sectionIndexTitles][section];
返回0;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
UILocalizedIndexedCollation*排序=[UILocalizedIndexedCollation currentCollation];
返回[[排序规则节索引]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
NSArray*animalsInSection=[self.animes objectAtIndex:section];
返回animalsensition.count;
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)表格视图{
UILocalizedIndexedCollation*排序=[UILocalizedIndexedCollation currentCollation];
返回[排序规则节索引];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell=[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray*animalsInSection=[self.animes对象索引:indexath.section];
cell.textlab.text=animalsecurition[indexPath.row][@“name”];
返回单元;
}
@结束

我希望这就是你想要做的。

我认为这个代码远远不是我需要的。首先,我不使用这个代码:{“B”:[@“熊”,“黑天鹅”,“水牛”,“C”:[@“骆驼”,“凤头鹦鹉”]……等等,因为我需要的是如何制作一本能够使这本字典成为动物的字典={(id=“1”,name=“熊”,“images=“bear321.jpg”),(id=“2“,name=“Dog”,images=“puppy.jpg”),(id=“3”,name=“GOOSE”,images=“null”),…等等,}类似于前者。第二,我需要一本字典来获取我的图像链接,但仍然按照名称排列。@Inase-36无论如何感谢您的帮助,希望您理解我的需要:'(在这种情况下,您可以使用从响应中获得的相同数组来创建具有类似索引的tableview。是否希望我这样做。是的,如果可以,请帮助我,因为我不知道如何创建它:(@insane-36请看我的编辑。我想你得到的响应类似于allAnimals,必须使用UIIndexedCallation将其转换为适当的分段对象,因此很容易。如果我有静态动物,这段代码就可以工作,如果我可以创建新动物并添加新图像呢?也许NSMutableArray和addObject函数可以为静态数组。到目前为止,我还没有问题。但是,我还需要显示列表中的动物图像,就在名称之前,我不想让马有猫的图像,所以,我需要对图像进行排序。我还没有尝试你的代码,但它似乎还没有显示图像。@ENASE-36你认为呢?
@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