SBJSON 与tableview

1.导入两个库SDimage,SBJSON
引入两个头文件
#import "SBJson.h"
#import "UIImageView+Webcache.h"

2.主要代码
Nsstring *urlstr=@"http://192.168.88.8/sns/my/user_list.php?page=1&number=500";
NSURL *url=[NSURL URLWithString:urlstr];
NSData *data=[NSData dataWithContentsOfURL:url];
//以上几步是根据网址转化为url,然后转化为2进制数据
userArrays=[[NSMutableArray alloc] init];
NSDictionary *resultDict=[data JSONValue];
//利用json解析库解析data,并且用字典存起来

NSArray *usersArray=[resultDict objectForKey:@"users"];
for (NSDictionary *userDict in usersArray) {
NSLog(@" %@",userDict);
UserItem *user=[[[UserItem alloc] init] autorelease];
//自定义了模型类,并且把每一组取出的一套数据存入uitableview中
user.username=[userDict objectForKey:@"username"];
user.realname=[userDict objectForKey:@"realname"];
user.userId=[userDict objectForKey:@"uid"];
user.imageurl=[Nsstring stringWithFormat:@" %@%@",@"http://192.168.88.8/sns",[userDict objectForKey:@"headimage"]]; [userArrays addobject:user]; 3.解释下需要哪个模型类以及uitableviewcell的定制 3.1模型类代码: 1. UserItem.h文件: @interface UserItem : NSObject { Nsstring *username; Nsstring *imageurl; UIImage *image; Nsstring *realname; NSNumber *userId; } @property (nonatomic,copy)Nsstring *username; @property (nonatomic,copy)Nsstring *imageurl; @property (nonatomic,retain)UIImage *image; @property (nonatomic,copy) Nsstring *realname; @property (nonatomic,retain) NSNumber *userId; @end 注意:使用@synthesize实现时,@property可以指定setter函数使用retain,copy或assign。assign一般用在属性一定会随着对象的消亡而消亡的,比如controller的view,view的delegate。 2.User.m文件代码: @implementation UserItem @synthesize username,image,imageurl; @synthesize realname; @synthesize userId; -(void)dealloc { self.realname=nil; self.username=nil; self.image=nil; self.imageurl=nil; self.userId=nil; [super dealloc]; } @end 3.uitableviewcell定制代码:定制在uitableview中每一组中每一行所显示内容 一般情况下都是xib形式自己定制。拖拉建立几个连接。话说我对这个拖控件都不太熟悉。 1.FriendViewCell.h文件: #import <UIKit/UIKit.h> @interface FriendViewCell : UITableViewCell { IBOutlet UILabel *mUserName; IBOutlet UILabel *mRealName; IBOutlet UIImageView *mHeadImage; } @property (nonatomic,readonly) IBOutlet UILabel *mUserName; @property (nonatomic,readonly) IBOutlet UILabel *mRealName; @property (nonatomic,readonly) IBOutlet UIImageView *mHeadImage; @end 2.FriendViewCell.m文件:(其实就是认不需要改动,只是加入几个@synthesize mUserName,mRealName;@synthesize mHeadImage;) 代码: @implementation FriendViewCell @synthesize mUserName,mRealName; @synthesize mHeadImage; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(Nsstring *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end 3.在FriendView界面中: 根据你创建的对象是继承那个类时,添加内容自然不一样。 如果继承的是uitableviewcontroller,则自然不用写哪些代理文件,也不用创建什么uitableview控件。 可以直接使用哪些方法:比如:numberOfRowsInSection,numberOfSectionsInTableView,cellForRowAtIndexPath。 这里显示我们将要呈现的界面: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //这些都是固定写法。 static Nsstring *CellIdentifier = @"Cell"; FriendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { //cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; //两种不同的方式 cell=[[[NSBundle mainBundle] loadNibNamed:@"FriendViewCell" owner:self options:nil] lastObject]; } UserItem *user=[userArrays objectAtIndex:indexPath.row]; cell.mUserName.text=user.username; cell.mRealName.text=user.realname; [cell.mHeadImage setimageWithURL:[NSURL URLWithString:user.imageurl] placeholderImage:[UIImage imageNamed:@"Placeholder"]]; //这个placeholder只是命个名而已 // Configure the cell... return cell; }

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...