Ios 在目标c的自定义UITable视图中从服务获取数据
.我是IOS新手。我正在制作一个应用程序,希望在Ios 在目标c的自定义UITable视图中从服务获取数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,.我是IOS新手。我正在制作一个应用程序,希望在UITableView 我看过很多博客和帖子,都是关于以自定义方式获取数据的,但我没有得到答案。我想在UIImageView中显示一个图像,并在label from service中显示一些标签值。我使用内置服务获取数据。 有许多post与自定义服务器上的静态数据加载相关。有谁能指导我如何从服务加载自定义样式UItable视图中的数据吗?为此,您需要同时遵循和教程 一个演示如何自定义UITableViewCell,另一个演示如何使用从服务器获取的数
UITableView
我看过很多博客和帖子,都是关于以自定义方式获取数据的,但我没有得到答案。我想在
UIImageView
中显示一个图像,并在label from service中显示一些标签值。我使用内置服务获取数据。
有许多post与自定义服务器上的静态数据加载相关。有谁能指导我如何从服务加载自定义样式UItable视图中的数据吗?为此,您需要同时遵循和教程 一个演示如何自定义
UITableViewCell
,另一个演示如何使用从服务器获取的数据填充UITableView
。你必须分步做
首先,设计自定义的UITableViewCell
然后,按照Mike的教程学习如何从API调用设置单元格上的数据
您将使用NSURLSession
进行API调用。
下面学习如何进行API调用
浏览这些链接
我们只能在代码量很少的情况下帮助您进行调试。但无法发布完整功能的代码
希望这些链接能对你有所帮助 我多少能理解你的问题。我的答案在这里 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": @"Available",
@"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];
}
#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;
}
查看我的自定义单元格图像视图
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
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
更新你的问题到目前为止,你已经尝试使你的观点清楚。我已经跟随了一个博客,但我不明白我如何可以这样做的自定义风格@SushilSharmaSo,基本上您希望从服务器获取数据并将数据填充到tableView中。是吗?我建议您遵循许多可用教程中的一个,即,这一个是的,但具有自定义样式,具有名称标签、价格标签、位置标签和图像视图中的图像@我用的是故事板而不是笔尖,你能为故事板编辑它吗?我无法测试@USER3182143我只在故事板中使用了上面的代码,请检查。对不起,好的,让我试试@USER3182143我使用uiview控制器而不是nib,我在其中使用uitableview@USER3182143使用.h、.m、.xib创建单独的cstom UItable视图单元格。
#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;
}