从Objective c中的自定义UITable View中获取服务中的数据

时间:2021-07-21 05:15:29

enter image description here .Im new to IOS. I am making an app where I want to get data from there in UITableView.
I have seen many blogs and post related to getting data in custom style, but I don't get my answer. I want to show an image in UIImageView and some labels values in label from service. Im using built in service to get data. There are many post related to static data loading on custom. Can anyone guide how can I load data in my own custom style UItable VIEW FROM SERVICE?

在这里输入图像描述。我是IOS新手。我正在创建一个应用程序,我想从UITableView中获取数据。我见过许多博客和帖子与自定义风格的数据有关,但我没有得到我的答案。我想在UIImageView中显示一个图像,并在服务的标签中显示一些标签值。我使用内置服务来获取数据。在自定义上有许多与静态数据加载相关的帖子。任何人都可以指导如何在我自己的自定义样式UItable VIEW FROM SERVICE中加载数据?

2 个解决方案

#1


1  

Somewhat I can understand your question.My answer is here

我可以理解你的问题。我的答案就在这里

FindHomeViewController.m

FindHomeViewController.m

#import "FindHomeViewController.h"
#import "DataTableViewController.h"

@interface FindHomeViewController ()

@end

@implementation FindHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)Search:(id)sender {  
   //Getting response from server
   NSDictionary *parameters = @{
                             @"country": @"UAE",
                             @"city": @"Dubai",
                             @"propertytype": @"Office",
                             @"propertystatus": @"Av‌​ailable",
                             @"propertyarea" : @"Kanal",
                             @"minprice" : @"800",
                             @"maxprice" : @"900"
                             };

    NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.pk.house/app_webservices/get_properties.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request
                                                             fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                              if(data != nil)
                                                              {
                                                               NSError *parseError = nil;
                                                               //If the response is in dictionary format
                                                              NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                                                               NSArray *arr=[dictionary valueForKey:@"property_data"];
                                                             NSLog(@"arr:%@",arr);

                                                             //Updating UIMain Thread
                                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                               UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                                               DataTableViewController *vc = [sb instantiateViewControllerWithIdentifier:@"DataTableViewController"];
                                                               vc.arrResprev = [arr mutableCopy];
                                                               [self.navigationController pushViewController:vc animated:YES];

                                                               });

                                                             }
                                                             else
                                                                    NSLog(@"Data returned the parameter is nil here");
                                                         }];
                                                         [dataTask resume];
}

See my Custom Cell Image View

查看我的自定义单元格图像视图

从Objective c中的自定义UITable View中获取服务中的数据

CustomeCell.h

CustomeCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell


@property (nonatomic,strong) IBOutlet UILabel *nameLabel;
@property (nonatomic,strong) IBOutlet UILabel *priceLabel;
@property (nonatomic,strong) IBOutlet UILabel *locationLabel;

@property (nonatomic,strong) IBOutlet UIImageView *imgvwRes;



@end

CustomCell.m

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

DataTableViewController.h

DataTableViewController.h

#import <UIKit/UIKit.h>

@interface DataTableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *tvCustomers;
@property (strong, nonatomic) NSMutableArray *listOfCustomers;
@property (strong, nonatomic) NSMutableArray *arrResprev;



@end

DataTableViewController.m

DataTableViewController.m

#import "DataTableViewController.h"
#import "CustomCell.h"


@interface DataTableViewController ()

@end

@implementation DataTableViewController


@synthesize tvCustomers,arrResprev,listOfCustomers;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    listOfCustomers = [[NSMutableArray alloc]init];
    listOfCustomers = arrResprev;
    [tvCustomers reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listOfCustomers.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 134;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tvCustomers dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    if(cell == nil){
        cell = nib[0];
    }
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"dealer_name"]];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"price"]];
    cell.locationLabel.text = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"location"]];
    NSString *strImgURL = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"images"]];
    NSError* error = nil;
    NSURL *fileURL = [NSURL fileURLWithPath:strImgURL];
    NSData* data = [NSData dataWithContentsOfURL:fileURL options:NSDataReadingUncached error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    } else {
        NSLog(@"Data has loaded successfully.");
    }
    UIImage *img = [[UIImage alloc] initWithData:data];
    cell.imgvwRes.image = omg;
    return cell;
}

#2


0  

For this you need to follow both Appcoda and mikesknowledgebase tutorials .

为此,您需要遵循Appcoda和mikesknowledgebase教程。

One shows how to customize UITableViewCell and other shows how to populate UITableView with data fetched from server. You will have to do it in steps.

一个展示了如何自定义UITableViewCell,以及其他如何使用从服务器获取的数据填充UITableView。你必须分步完成。

First, design the Custom UITableViewCell.

首先,设计Custom UITableViewCell。

Then, follow Mike's tutorial to learn how to set data on cell from API call.

然后,按照Mike的教程学习如何在API调用中设置单元格数据。

You will use NSURLSession to make API calls. Follow Link to learn how to make an API call.

您将使用NSURLSession进行API调用。按照链接了解如何进行API调用。

Go through these links.

浏览这些链接。

We can only help you in debugging where little amount of code will work. But cannot post code for the complete functionality.

我们只能帮助您调试少量代码的工作方式。但无法发布完整功能的代码。

Hope these links will help you.

希望这些链接能为您提供帮助。

#1


1  

Somewhat I can understand your question.My answer is here

我可以理解你的问题。我的答案就在这里

FindHomeViewController.m

FindHomeViewController.m

#import "FindHomeViewController.h"
#import "DataTableViewController.h"

@interface FindHomeViewController ()

@end

@implementation FindHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)Search:(id)sender {  
   //Getting response from server
   NSDictionary *parameters = @{
                             @"country": @"UAE",
                             @"city": @"Dubai",
                             @"propertytype": @"Office",
                             @"propertystatus": @"Av‌​ailable",
                             @"propertyarea" : @"Kanal",
                             @"minprice" : @"800",
                             @"maxprice" : @"900"
                             };

    NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.pk.house/app_webservices/get_properties.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request
                                                             fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                              if(data != nil)
                                                              {
                                                               NSError *parseError = nil;
                                                               //If the response is in dictionary format
                                                              NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                                                               NSArray *arr=[dictionary valueForKey:@"property_data"];
                                                             NSLog(@"arr:%@",arr);

                                                             //Updating UIMain Thread
                                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                               UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                                               DataTableViewController *vc = [sb instantiateViewControllerWithIdentifier:@"DataTableViewController"];
                                                               vc.arrResprev = [arr mutableCopy];
                                                               [self.navigationController pushViewController:vc animated:YES];

                                                               });

                                                             }
                                                             else
                                                                    NSLog(@"Data returned the parameter is nil here");
                                                         }];
                                                         [dataTask resume];
}

See my Custom Cell Image View

查看我的自定义单元格图像视图

从Objective c中的自定义UITable View中获取服务中的数据

CustomeCell.h

CustomeCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell


@property (nonatomic,strong) IBOutlet UILabel *nameLabel;
@property (nonatomic,strong) IBOutlet UILabel *priceLabel;
@property (nonatomic,strong) IBOutlet UILabel *locationLabel;

@property (nonatomic,strong) IBOutlet UIImageView *imgvwRes;



@end

CustomCell.m

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

DataTableViewController.h

DataTableViewController.h

#import <UIKit/UIKit.h>

@interface DataTableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *tvCustomers;
@property (strong, nonatomic) NSMutableArray *listOfCustomers;
@property (strong, nonatomic) NSMutableArray *arrResprev;



@end

DataTableViewController.m

DataTableViewController.m

#import "DataTableViewController.h"
#import "CustomCell.h"


@interface DataTableViewController ()

@end

@implementation DataTableViewController


@synthesize tvCustomers,arrResprev,listOfCustomers;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    listOfCustomers = [[NSMutableArray alloc]init];
    listOfCustomers = arrResprev;
    [tvCustomers reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listOfCustomers.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 134;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tvCustomers dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    if(cell == nil){
        cell = nib[0];
    }
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"dealer_name"]];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"price"]];
    cell.locationLabel.text = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"location"]];
    NSString *strImgURL = [NSString stringWithFormat:@"%@",[[listOfCustomers objectAtIndex:indexPath.row]objectForKey:@"images"]];
    NSError* error = nil;
    NSURL *fileURL = [NSURL fileURLWithPath:strImgURL];
    NSData* data = [NSData dataWithContentsOfURL:fileURL options:NSDataReadingUncached error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    } else {
        NSLog(@"Data has loaded successfully.");
    }
    UIImage *img = [[UIImage alloc] initWithData:data];
    cell.imgvwRes.image = omg;
    return cell;
}

#2


0  

For this you need to follow both Appcoda and mikesknowledgebase tutorials .

为此,您需要遵循Appcoda和mikesknowledgebase教程。

One shows how to customize UITableViewCell and other shows how to populate UITableView with data fetched from server. You will have to do it in steps.

一个展示了如何自定义UITableViewCell,以及其他如何使用从服务器获取的数据填充UITableView。你必须分步完成。

First, design the Custom UITableViewCell.

首先,设计Custom UITableViewCell。

Then, follow Mike's tutorial to learn how to set data on cell from API call.

然后,按照Mike的教程学习如何在API调用中设置单元格数据。

You will use NSURLSession to make API calls. Follow Link to learn how to make an API call.

您将使用NSURLSession进行API调用。按照链接了解如何进行API调用。

Go through these links.

浏览这些链接。

We can only help you in debugging where little amount of code will work. But cannot post code for the complete functionality.

我们只能帮助您调试少量代码的工作方式。但无法发布完整功能的代码。

Hope these links will help you.

希望这些链接能为您提供帮助。