tableView的网络数据获取

来源:互联网 发布:php 编写服务器端 编辑:程序博客网 时间:2024/06/05 19:48

ViewController.h

复制代码
 1 #import <UIKit/UIKit.h> 2  3 @interface ViewController : UIViewController 4 <UITableViewDataSource> 5 { 6     UITableView* _tableView; 7     NSMutableArray* dataArray; 8 } 9 10 @end
复制代码

ViewController.m

复制代码
 1 #import "ViewController.h" 2 #import "UserItem.h" 3 #import "SBJson.h" 4  5 @implementation ViewController 6 #pragma mark - viewDidLoad Event 7 -(void)loadData 8 { 9     dataArray = [[NSMutableArray alloc] init];//这个地方记着alloc10     11     //下载数据12     NSString* urlStr = @"http://192.168.88.8/sns/my/user_list.php";13     NSURL* url = [NSURL URLWithString:urlStr];14     NSString* jsonStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];15     //解析数据16     NSDictionary* jsonDic = [jsonStr JSONValue];17     //取出users数组18     NSArray* usersArray = [jsonDic objectForKey:@"users"];19     for (NSDictionary* dic in usersArray) {20         //取出每一个用户的信息21         NSString* nickName = [dic objectForKey:@"username"];22         NSString* imageUrl = [dic objectForKey:@"headimage"];23         //封装起来24         UserItem* item = [[UserItem alloc] init];25         item.nickName = nickName;26         item.imageUrl = imageUrl;27         [dataArray addObject:item];28         [item release];29     }30 31 }32 -(void)initTable33 {34     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];35     _tableView.dataSource = self;36     [self.view addSubview:_tableView];37     [_tableView release];38 }39 40 #pragma mark - viewDidLoad41 - (void)viewDidLoad42 {43     [super viewDidLoad];44     //加载数据45     [self loadData];46     //创建表格47     [self initTable];48 }49 50 51 #pragma mark - UITableViewDataSource52 53 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{54     return dataArray.count;55 }56 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{57     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];58     if (cell == nil) {59         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ID"] autorelease];60             61         //cell上添加控件62         UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, 30)];63         imageView.tag = 101;64         [cell.contentView addSubview:imageView];65         [imageView release];66         67         UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(40, 5, 200, 40)];68         label.tag = 102;69         label.backgroundColor = [UIColor clearColor];70         [cell.contentView addSubview:label];71         [label release];72 73     }74     UIImageView* imageView = (UIImageView*)[cell.contentView viewWithTag:101];75     UILabel* label = (UILabel*)[cell.contentView viewWithTag:102];76     77     //取得数据78     UserItem* item = [dataArray objectAtIndex:indexPath.row];79     80     //赋值81     NSString* imageUrl = [NSString stringWithFormat:@"http://192.168.88.8/sns%@",item.imageUrl];82     imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];83     label.text = item.nickName;84     85     return cell;86 }87 88 @end
复制代码

 

 

UserItem.h

 

复制代码
1 #import <Foundation/Foundation.h>2 3 @interface UserItem : NSObject4 5 @property (nonatomic,retain)NSString* nickName;6 @property (nonatomic,retain)NSString* imageUrl;7 8 @end
复制代码

 

Useritem.m

复制代码
 1 #import "UserItem.h" 2  3 @implementation UserItem 4 @synthesize nickName = _nickName; 5 @synthesize imageUrl = _imageUrl; 6  7 - (void)dealloc{ 8     self.nickName = nil; 9     self.imageUrl = nil;10     11     [super dealloc];12 }13 14 @end
复制代码

0 0
原创粉丝点击