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
- tableView的网络数据获取
- 【Swift】Alamofile网络请求数据更新TableView的坑
- 网络数据获取的封装
- 获取网络数据的封装
- 获取tableview的某一行
- tableView展示数据的过程
- 如何统一处理tableView无数据,网络请求失败的占位图
- 获取网络上的json数据
- Android中网络获取数据的方法
- 简单的获取网络数据 测试使用
- 简单的异步获取网络数据
- 工具类 获取网络数据的方法
- NetDataUtil_获取网络数据的工具类
- python本地与网络数据的获取
- WPF DevExpress GridControl TableView 全选 和反选 以及获取选中行的数据
- 获取网络数据
- 获取网络数据
- 获取网络数据
- CEPH中Monitor对OSD状态的监控
- Excel - SUMIF函数
- 计算机视觉开源代码集合
- cug1559 最少操作次数
- Ubuntu15 in Virtualbox 无法全屏
- tableView的网络数据获取
- 每天一个小知识点2(sass总结)
- 腾讯SNG 7月25日——开发类专场招聘会
- ubuntu64位adt bound创建项目找不到R文件
- 凸包模板
- Java程序的编码规范
- brackets前端开发工具添加html自动格式format插件
- JAVA学习(一)安装和测试
- 黑马程序员——我的学习感悟....