加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SBJSON 与tableview

发布时间:2020-12-16 19:24:03 所属栏目:百科 来源:网络整理
导读:1.导入两个库SDimage,SBJSON 引入两个头文件: #import "SBJson.h" #import "UIImageView+WebCache.h" 2.主要代码: NSString *urlstr=@"http://192.168.88.8/sns/my/user_list.php?page=1number=500"; NSURL *url=[NSURL URLWithString:urlstr]; NSData *da
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; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读