2015-10-31 3 views
-1
http://api.testmy.co/user/files 

Мне нужно получить данные и отобразить эти данные в моем представлении таблицы, например grid view. Я новичок в этом типе обработки api, получите запрос и все. Если кто-нибудь может объяснить об этом "Get type request" &, как отображать данные в моем представлении таблицы, например, в виде сетки. У меня есть только этот url (например, только выше url).Получить запрос - для отображения данных по api

Может ли какая-либо помощь помочь мне с некоторыми учебниками или git-хабом или с любой идеей об этом. Спасибо заранее!

ответ

1

Я помогу вам получить ответ. После того как вы сохраните данные ответа в массиве или в dictionar, вы можете показать табличное представление.

-(void)getResponse 
{ 
//just give your URL instead of my URL 
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]]; 

[request setHTTPMethod:@"GET"]; 

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 

NSError *err; 

NSURLResponse *response; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

//You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER.If you do this clearly you can get the correct results.  

//After that it depends upon the json format whether it is DICTIONARY or ARRAY 

//If it(RESPONSE) starts with dictionary({...}),you need to write coding blow like this 

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

NSArray *array=[[jsonDict objectForKey:@"search_api"]objectForKey:@"result"]; 

//But if it(RESPONSE) starts with array([...]),you need to write coding blow like this 

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 
} 

Если вы хотите получить сетку, лучше использовать CollectionView.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self getResponse]; 
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; 
    //Register the custom cell for collection view 
    UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; 
    [collectionViewHorizontalVertical registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"]; 
} 

//Collection View Delegates method 
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return jsonArray.count; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cvCell"; 
CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    cell.imgViewCollection.image = [UIImage imageNamed:[jsonArray objectAtIndex:indexPath.row]]; 

    return cell; 
} 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(200, 200); //Please give your required size 
} 
+0

Спасибо за ваш код bro..But я даже не знаю, как начать с моей URL для получения данных и отображения – jj1

+0

так только я попросил любой учебник или демо-проект – jj1

+0

нормально bro.let дать мне какой-то демонстрационный проект или ссылку на учебник, чтобы сделать с нуля для моего вопроса – jj1

Смежные вопросы