从网站获取用户数据写进tableView
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];//这个地方记着alloc 10 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)initTable 33 { 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 - viewDidLoad 41 - (void)viewDidLoad 42 { 43 [super viewDidLoad]; 44 //加载数据 45 [self loadData]; 46 //创建表格 47 [self initTable]; 48 } 49 50 51 #pragma mark - UITableViewDataSource 52 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 : NSObject 4 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