我试图从api获取图像并将其显示在UITableView上。但是,列表分组标题后图像无法正确显示。我不知道如何修复它并为每行记录显示正确的图像。
对于我的图片,我们会获得http://www.domain_name.com/Images/Merchant/abc.jpg之类的网址链接。
非常感谢您的帮助。谢谢。
#import <Foundation/Foundation.h>
#import "Friend_ViewController.h"
#import "FriendDetails_ViewController.h"
#import "AFHTTPSessionManager.h"
#import "UIImageView+AFNetworking.h"
@interface Friend_ViewController () <UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate, UISearchResultsUpdating>
@end
@implementation Friend_ViewController{
NSMutableArray *tableData;
UITableView *tableView;
BOOL isFiltered;
NSMutableArray *stateNamesArray;
NSMutableArray *stateNamesArrayImage;
NSMutableArray *searchResult;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.sectionIndexColor = ThemeBlueColor;
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
self.tableView.rowHeight = 60;
isFiltered =false;
self.navigationItem.title=@"Friends";
UIImage* image3 = [UIImage imageNamed:@"Add_Friend"];
CGRect frameimg = CGRectMake(0,0, image3.size.width -10, image3.size.height);
UIButton *btn_add_friends = [[UIButton alloc] initWithFrame:frameimg];
[btn_add_friends setBackgroundImage:image3 forState:UIControlStateNormal];
[btn_add_friends addTarget:self action:@selector(addFriendButtonDidPressed:)
forControlEvents:UIControlEventTouchUpInside];
[btn_add_friends setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *btn_add_friends_item =[[UIBarButtonItem alloc] initWithCustomView:btn_add_friends];
self.navigationItem.rightBarButtonItem =btn_add_friends_item;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://api.domain_name.com/api/merchant" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
stateNamesArray=[responseObject valueForKey:@"merchant_name"];
stateNamesArrayImage=[responseObject valueForKey:@"image"];
NSInteger indexLabelLettersCount = [[UILocalizedIndexedCollation currentCollation] sectionTitles].count;
NSMutableArray *allSections = [[NSMutableArray alloc] initWithCapacity:indexLabelLettersCount];
for (int i = 0; i < indexLabelLettersCount; i++) {
[allSections addObject:[NSMutableArray array]];
}
for(NSString *theState in stateNamesArray){
NSInteger sectionNumber = [[UILocalizedIndexedCollation currentCollation] sectionForObject:theState collationStringSelector:@selector(lowercaseString)];
[allSections[sectionNumber] addObject:theState];
}
NSMutableArray *sortedArray = [[NSMutableArray alloc] init];
self.activeSectionIndices = [NSMutableDictionary dictionary];
self.activeSectionTitles = [NSMutableArray array];
for (int i = 0; i < indexLabelLettersCount; i++) {
NSArray *statesForSection = allSections[i];
NSString *indexTitleForSection = [[UILocalizedIndexedCollation currentCollation] sectionTitles][i];
if (statesForSection.count > 0) {
[self.activeSectionTitles addObject:indexTitleForSection];
NSArray *tmpSectionStates = allSections[i];
tmpSectionStates = [tmpSectionStates sortedArrayUsingSelector:@selector(compare:)];
[sortedArray addObject:tmpSectionStates];
}
NSNumber *index = [NSNumber numberWithInt:MAX(self.activeSectionTitles.count - 1, 0)];
self.activeSectionIndices[indexTitleForSection] = index;
}
tableData = sortedArray;
self.tableView.sectionHeaderHeight = 25;
self.navigationController.navigationBar.translucent = NO;
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.navigationController.navigationBar.translucent = NO;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = ThemeLightGrayColor;
self.searchController.searchBar.layer.borderWidth = 0;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
[self.view addSubview:self.searchBar];
searchResult = [NSMutableArray arrayWithCapacity:[stateNamesArray count]];
[self.tableView reloadData];
} failure:^(NSURLSessionTask *operation, NSError *error) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Error Retrieving Ninjas"
message:[error localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];`enter code here`
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleCancel
handler:nil];
[alertVC addAction:okAction];
[self presentViewController:alertVC animated:YES completion:nil];
}];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(nonnull NSString *)searchText
{
//[searchText removeAllObjects];
if(searchText.length == 0)
{
isFiltered = NO;
}
else
{
isFiltered = YES;
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF CONTAINS %@",
searchText];
searchResult = [stateNamesArray filteredArrayUsingPredicate:resultPredicate];
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView: (UITableView *) tableView{
return isFiltered ? 1 : tableData.count;
}
- (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(isFiltered){
return searchResult.count;
}
else
{
NSArray *arr = tableData[section];
return arr.count;
}
}
- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if(isFiltered){
cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[stateNamesArrayImage objectAtIndex:indexPath.row]];
}
else{
NSArray *arr = tableData[indexPath.section];//get the current section array
cell.textLabel.text = arr[indexPath.row];//get the row for the current section
NSURL *url = [NSURL URLWithString:[stateNamesArrayImage objectAtIndex:indexPath.row]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
FriendDetails_ViewController *FriendDetails_ViewControl = [[FriendDetails_ViewController alloc] init];
[self.navigationController pushViewController:FriendDetails_ViewControl animated:YES];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(isFiltered)
{
return nil;
}
return self.activeSectionTitles[section];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSArray *letterIndexLabels = [[UILocalizedIndexedCollation currentCollation] sectionTitles];
return letterIndexLabels;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return [self.activeSectionIndices[title] integerValue];
}
@end