add images from MySQL database on sorted UITableViewCell

邮差的信 提交于 2019-12-25 05:19:17

问题


i'm new to Xcode and Objective-c and i wanna make a contact list which sorted alphabetically also have images in it. Please check this for reference.

This code tells me everything i need but the problem is i use name and images from MySQL instead of hardcoded like that.

I have imported the data to NSDictionary and this is the result:

audiences=({name="ABDUL",id="1",photo="asd.png"},{name="ABDUL",id="2",photo="qwe.png"});

What i need to do to add the name and photo (only that 2 items, without id) to UITableViewCell? I used this code before but only show the sorted name and i don't have any idea how to call the images.

This is how i parse the name so i can grab it for the list:

NSMutableDictionary* audict = [NSMutableDictionary dictionary];
               for (NSDictionary* person in dataDict[@"audience"]){
                  NSString* name = person[@"name"];
                  if (![name length])
                     continue;
                  NSRange range = [name rangeOfComposedCharacterSequenceAtIndex:0];
                  NSString* key = [[name substringWithRange:range] uppercaseString];
                  NSMutableArray* list = audict[key];
                  if (!list){
                     list = [NSMutableArray array];
                     [audict setObject:list forKey:key];
                  }
                  [list addObject:name];

                  audiences=audict;
                  audienceSectionTitles = [[audiences allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
                  audienceIndexTitles = @[@"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"];

This is where the code executed

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // Return the number of rows in the section.
   NSString *sectionTitle = [audienceSectionTitles objectAtIndex:section];
   NSArray *sectionAudience = [audiences objectForKey:sectionTitle];
   return [sectionAudience count];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

   // Configure the cell...
   NSString *sectionTitle = [audienceSectionTitles objectAtIndex:indexPath.section];
   NSArray *sectionAudience = [audiences objectForKey:sectionTitle];
   NSString *audience = [sectionAudience objectAtIndex:indexPath.row];
   UIFont *myFont = [ UIFont fontWithName: @"Helvetica" size: 12.0 ];
   cell.textLabel.font  = myFont;
   cell.textLabel.text = audience;
   cell.imageView.image = [UIImage imageNamed:[self getImageFilename:audience]];
   return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   return audienceIndexTitles;
}

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

And here where i need to put my the image strings:

- (NSString *)getImageFilename:(NSString *)audience
{
   NSString *imageFilename = @"profile.png";

   return imageFilename;
}

Please i need your help ^^;


回答1:


Are the images on your database? MySQl you mean, are you using sqlite or Core Data? This is the code for a simple select in sqlite:

const unsigned char *myBlob = sqlite3_column_blob(sqlite3_stmt *, column); if(myBlob != NULL){ value = [NSString stringWithUTF8String:(const char *)myBlob]; }



来源:https://stackoverflow.com/questions/25521785/add-images-from-mysql-database-on-sorted-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!