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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值