这个页面要讲述的是用户的粉丝列表,下面是效果图:

可以看到这个视图明显也是一个tableview,在每一个cell中包含的有三个部分的内容:粉丝头像image,粉丝昵称label,我和粉丝之间的相互关注情况button。
在这个页面我们主要处理的内容有:① 粉丝列表数据的获取
②tableview视图界面的组织(重点是:添加关注和取消关注)
(1)首先是获取粉丝列表数据,这部分内容没有什么好说的,就是些JSON数据,然后解析。调用的API:https://api.weibo.com/2/friendships/followers.json。其中有一个点是需要注意的。我们注意到其中API请求的参数:
count |
false |
int |
单页返回的记录条数,默认为50,最大不超过200。 |
cursor |
false |
int |
返回结果的游标,下一页用返回值里的next_cursor,上一页用previous_cursor,默认为0。 |
可以知道一次数据请求是以页的方式返回的,每一页返回count条数的粉丝记录,这个和前面微博内容的返回的差不多的,都是以页为单位返回,但是这里有一个比较特殊的是,在这个API的数据请求中没有page这个参数,而是用到了cursor这个参数,这个参数开始是0,以后每一次请求后获取的数据中就有next_cursor这个返回值,用它来为cursor赋值,就可以继续获取接下去的数据了,如果粉丝数据已经全部获取到了,那么这个值返回就又是0了。那么我们在继续加载的时候就可以添加一个判断,判断next_cursor是否为0,如果不为0,说明粉丝列表还没有加载完毕,可以继续请求加载;如果为0,则说明所有的粉丝数据都已经加载了,这时就不要继续再加载了。
下面通过代码看看吧!
- - (void) loadFollersDataWithCursor:(int) cursor {
-
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- dispatch_sync(dispatch_get_global_queue(0, 0), ^{
-
- hud = [[MBProgressHUD alloc] init];
- hud.dimBackground = YES;
- hud.labelText = @"正在加载数据...";
- [hud show:YES];
- [self.view addSubview:hud];
-
-
- NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[InfoForSina returnFollowersUrlStringWithCursor:cursor]]];
- NSError *error;
- NSData *followersData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
-
-
- NSString *follwersString = [[NSString alloc] initWithData:followersData encoding:NSUTF8StringEncoding];
-
- NSDictionary *followersDictionary = [follwersString objectFromJSONString];
- NSArray *followersArray = [followersDictionary objectForKey:@"users"];
-
- followersCount = [[followersDictionary objectForKey:@"total_number"] integerValue];
- nextCursor = [[followersDictionary objectForKey:@"next_cursor"] integerValue];
-
- for (NSDictionary *dictionary in followersArray) {
-
- User *followersUser = [[User alloc] init];
- [followersUser initUserWithDictionary:dictionary];
- [self.followersMutableArray addObject:followersUser];
-
- }
-
- });
- dispatch_sync(dispatch_get_main_queue(), ^{
-
- [self.tableView reloadData];
- [hud removeFromSuperview];
- });
-
- });
- }
-
-
- - (void) scrollViewDidScroll:(UIScrollView *)scrollView {
-
- CGPoint contentOffsetPoint = self.tableView.contentOffset;
-
- CGRect frame = self.tableView.frame;
-
- if (contentOffsetPoint.y == self.tableView.contentSize.height - frame.size.height)
- {
- if (nextCursor!=0) {
- [self loadFollersDataWithCursor:nextCursor];
- }
-
- else {
- MBProgressHUD *endHud = [[MBProgressHUD alloc] init];
- endHud.mode = MBProgressHUDModeText;
- endHud.labelText = @"提示";
- endHud.detailsLabelText = @"粉丝数据已全部加载!";
- [self.tableView addSubview:endHud];
- [endHud show:YES];
- [endHud hide:YES afterDelay:0.5];
- }
-
- }
- }
注意到其中的nextCursor这个参数,就是用来继续加载的关键参数,在开始的时候
nextCursor = 0;
[self
loadFollersDataWithCursor:nextCursor];
以后每一次加载数据的时候就用获取到的新值为它重新赋值。
(2)tableview视图界面的组织
这里粉丝头像和粉丝的昵称的显示就不多说了,很简单的。代码如下:
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"FansCell";
- FansCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- if (cell == nil) {
- cell = [[FansCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
-
-
- User *aUser = [[User alloc] init];
- aUser = [_followersMutableArray objectAtIndex:[indexPath row]];
-
- cell.nameLabel.adjustsFontSizeToFitWidth = YES;
- cell.nameLabel.text = aUser.nameString;
-
-
- cell.fansButtonLabel.titleLabel.adjustsFontSizeToFitWidth = YES;
- if (aUser.following) {
-
- [cell.fansButtonLabel setTitle:@"互相关注" forState:UIControlStateNormal];
- }
- else {
- [cell.fansButtonLabel setTitle:@"+关注" forState:UIControlStateNormal];
- }
-
-
- __block NSData *imageData;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- dispatch_sync(dispatch_get_global_queue(0, 0), ^{
- imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:aUser.profileImageURL]];
- });
-
- dispatch_sync(dispatch_get_main_queue(), ^{
- UIImage *image = [[UIImage alloc] initWithData:imageData];
- [cell.imageView setImage:image];
- });
- });
-
-
- CALayer *l = [cell.imageView layer];
- [l setMasksToBounds:YES];
- [l setCornerRadius:6.0];
-
-
- cell.fansUIDString = aUser.IDString;
-
- return cell;
- }
说明:
1、里面的头像我设置成圆角,这时需要#import
"QuartzCore/QuartzCore.h",其他都可以看懂的吧。
2、其中获取头像的方法是:
- -(UIImage *) getImageFromURL:(NSString *)fileURL {
- UIImage * resultImage = [[UIImage alloc] init];
- NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
- resultImage = [UIImage imageWithData:data];
- return resultImage;
- }
在这一部分的内容中要值得说明的是我添加的那个按键的处理。首先说一下我这个按键的作用:我们在获取到的粉丝JSON数据中有:follow_me和following这两个项,前者表示的是该粉丝是否关注我,显然我的粉丝肯定是关注我的啦,所以一直返回是true;后者表示的是我是否关注这个粉丝,有关注返回true,否则就返回false。所以我们就可以在这里面做点事情了。
如果我没有关注这个粉丝即following是false的话,button的作用就是可以添加对这个粉丝的关注,button的label就是“+关注”。
如果我关注了这个粉丝即following是true的话,button的作用就是可以取消对这个粉丝的关注,button的label就是“取消关注”;

由于有这部分的处理,所以我定义了一个Fanscell类专门是用于处理cell这部分的内容。
下面着重讲的是关注和取消关注粉丝的处理。
①、请求API及参数
添加关注的API:https://api.weibo.com/2/friendships/create.json
取消关注的API: https://api.weibo.com/2/friendships/destroy.json
二者调用时所需的参数除了access_token之外,还要粉丝的uid或者screen_name(二选一),注意前者是long
long (int64)类型,后者是string类型,我选用的是uid这个参数。
②、Fanscell类
其中包括的有:
- - (IBAction)fansButton:(id)sender;
- @property (weak, nonatomic) IBOutlet UIButton *fansButtonLabel;
- @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
- @property (weak, nonatomic) IBOutlet UIImageView *headImageView;
- @property (strong, nonatomic) NSString *fansUIDString;
其中的fansButton方法是用来处理关注和取消关注粉丝的。
下面先给出处理的过程代码再解说:
说明:
1、当按下这个button时会触发一个UIAlertView,提示是否确定关注和取消关注。

2、确定要关注或者取消关注粉丝的处理在UIAlertView的delegate方法中处理。
3、关注和取消关注粉丝的数据请求方式是POST,我们可以用ASIHTTPRequest来异步方式处理,也可以用不用这个类库,用系统自带的方法异步POST处理。代码中有我使用了这这两种方式。其中取消关注我是使用ASIHTTPRequest,而添加关注是使用系统自带的方法(当然注释部分也给出了ASIHTTPRequest的使用代码)。
注意,如果是使用系统自带的方式异步POST处理,我还添加了两个property:
@property
(strong, nonatomic) NSURLConnection *connection;
@property (strong,
nonatomic) NSMutableData *responseData;
通过代码可以清楚的看到,前一种处理方式十分简洁,后一种处理方式由于要实现delegate方法,所以感觉有点麻烦,二者孰好孰不好,自己感觉咯!
4、在数据请求结束我继续使用MBProgressHUD这个类库添加了一些提示框,表示关注是否成功或者取消关注是否成功。效果如下:

这个页面的处理差不多就是这样了!
ios sinaweibo 客户端(三)
原文:http://www.cnblogs.com/yulang314/p/3549550.html