自定义带网络请求的UITableView中tableHeaderView视图

接口文件

#import <UIKit/UIKit.h>
@class User;

@protocol ASIHTTPRequestDelegate;

@interface ProfileHeaderView : UIView <ASIHTTPRequestDelegate>

@property (nonatomic, retain) User *user;
@property (nonatomic, assign) UIViewController *controller;

@end



实现文件

#import "ProfileHeaderView.h"
#import "User.h"
#import <QuartzCore/QuartzCore.h>
#import "ASIHTTPRequest.h"
#import "FriendController.h"
#import "FollowerController.h"

//宏定义常量
#define kPadding 10
#define kIconWidth 100
#define kIconHeight 100
#define kCountButtonHeight 35
#define kCountSize 12
#define kCountButtonWidth 55
#define kNameSize 15
#define kDescSize 10
#define kBtnFriend 1
#define kBtnFollower 2
#define kGlobalBg [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1]

@interface ProfileHeaderView ()
{
    UIImageView *_icon;
    UILabel *_name;
    UILabel *_desc;
    UILabel *_status;
    
    UIButton *_friends;
    UIButton *_followers;
}

@end

@implementation ProfileHeaderView

- (id)init
{
    if (self = [super init])
    {
        CGSize winSize = [UIScreen mainScreen].bounds.size;
        //顶部
        UIImageView *topView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winSize.width, kIconHeight + 2 * kPadding)] autorelease];
        topView.image = [UIImage imageNamed:@"profile_cover_background.png"];
        [self addSubview:topView];
        
        //头像
        CGFloat iconX = kPadding;
        CGFloat iconY = kPadding;
        _icon = [[[UIImageView alloc] initWithFrame:CGRectMake(iconX, iconY, kIconWidth, kIconHeight)] autorelease];
        _icon.image = [UIImage imageNamed:@"avatar_default_big.png"];
        _icon.layer.cornerRadius = 5;
        _icon.layer.masksToBounds = YES;
        [topView addSubview:_icon];
        
        //昵称
        CGFloat nameX = iconX + kIconWidth + kPadding;
        CGFloat nameY = iconY;
        CGFloat nameWidth = winSize.width - nameX - kPadding;
        CGFloat nameHeight = kNameSize;
        _name = [[[UILabel alloc] init] autorelease];
        _name.frame = CGRectMake(nameX, nameY, nameWidth, nameHeight);
        _name.backgroundColor = [UIColor clearColor];
        _name.font = [UIFont systemFontOfSize:kNameSize];
        [topView addSubview:_name];
        
        //简介
        CGFloat descX = nameX;
        CGFloat descY = nameY + nameHeight + kPadding;
        CGFloat descWidth = nameWidth;
        CGFloat descheight = kIconHeight - descY;
        _desc = [[[UILabel alloc] init] autorelease];
        _desc.frame = CGRectMake(descX, descY, descWidth, descheight);
        _desc.font = [UIFont systemFontOfSize:kDescSize];
        _desc.backgroundColor = [UIColor clearColor];
        _desc.numberOfLines = 0;
        _desc.textColor = [UIColor whiteColor];
        [topView addSubview:_desc];
        
        //数目
        CGFloat countViewY = topView.frame.size.height;
        CGFloat countViewHeight = kCountButtonHeight + 2*kPadding;
        UIView *countView = [[[UIView alloc] initWithFrame:CGRectMake(0, countViewY, winSize.width, countViewHeight)] autorelease];
        countView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
        [self addSubview:countView];
        
        //关注
        CGFloat friendsX = kPadding;
        CGFloat friendsY = kPadding;
        _friends = [self buttonWithSelector:@selector(btnClick:) x:friendsX y:friendsY];
        _friends.tag = kBtnFriend;
        [countView addSubview:_friends];
        
        //粉丝
        CGFloat followersX = friendsX + kCountButtonWidth + kPadding;
        CGFloat followersY = friendsY;
        _followers = [self buttonWithSelector:@selector(btnClick:) x:followersX y:followersY];
        _followers.tag = kBtnFollower;
        [countView addSubview:_followers];
        
        //顶部的线
        CGFloat bottomHeight = 2;
        UIView *bottom = [[[UIView alloc] initWithFrame:CGRectMake(0, countViewHeight - bottomHeight, winSize.width, bottomHeight)] autorelease];
        bottom.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"profile_shadow_bottom.png"]];
        [countView addSubview:bottom];
        
        //微博数
        CGFloat statusX = 0;
        CGFloat statusY = countViewY + countViewHeight + 2;
        CGFloat statusHeight = kCountSize + kPadding;
        _status = [[[UILabel alloc] init] autorelease];
        _status.backgroundColor = kGlobalBg;
        _status.frame = CGRectMake(statusX, statusY, winSize.width, statusHeight);
        _status.font = [UIFont systemFontOfSize:kCountSize];
        [self addSubview:_status];
        
        self.frame = CGRectMake(0, 0, winSize.width, statusY + statusHeight);
    }
    return self;
}

- (void)dealloc
{
    [_user release];
    [super dealloc];
}

- (UIButton *)buttonWithSelector:(SEL)selector x:(CGFloat)x y:(CGFloat)y
{
    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(x, y, kCountButtonWidth, kCountButtonHeight)] autorelease];
    button.titleLabel.font = [UIFont systemFontOfSize:kCountSize];
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage resizeImage:@"skin_cell_background.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage resizeImage:@"skin_cell_background_highlighted.png"] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage resizeImage:@"skin_cell_background_highlighted.png"] forState:UIControlStateDisabled];
    return button;
}

#pragma mark -监听按钮点击

- (void)btnClick:(UIButton *)btn
{
    FriendshipController *vc = nil;
    if (btn.tag == kBtnFollower)
    {
        //粉丝
        vc = [[[FollowerController alloc] init] autorelease];
        vc.title = [NSString stringWithFormat:@"%@的粉丝", self.user.screenName];
    }
    else
    {
        //关注
        vc = [[[FriendController alloc] init] autorelease];
        vc.title = [NSString stringWithFormat:@"%@的关注", self.user.screenName];
    }
    vc.uid = self.user.uid;
    [self.controller.navigationController pushViewController:vc animated:YES];
}

#pragma mark -返回数目按钮的文字

- (NSString *)countText:(int)count title:(NSString *)title
{
    NSString *countTitle = nil;
    if (count < 10000)
    {
        countTitle = [NSString stringWithFormat:@"%i", count];
    }
    else
    {
        CGFloat countValue = count / 10000.0;
        countTitle = [NSString stringWithFormat:(countValue >= 100 ? @"%.0f万" : @"%.1f万"), countValue];
    }
    
    return [NSString stringWithFormat:@"%@\n%@", countTitle, title];
}

#pragma mark - user的setter

- (void)setUser:(User *)user
{
    if (_user != user)
    {
        [_user release];
        _user = [user retain];
        
        //下载图片
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:user.avatarLarge]];
        request.delegate = self;
        [request startAsynchronous];
        
        //设置名称
        _name.text = user.screenName;
        
        //设置简介
        NSString *descText = (user.descs == nil || [@"" isEqualToString:user.descs]) ? @"这个人比较懒,什么也没写" : [NSString stringWithFormat:@"简介:\n%@", user.descs];
        _desc.text = descText;
        
        //设置数目
        [_friends setTitle:[self countText:user.friendsCount title:@"关注"] forState:UIControlStateNormal];
        [_followers setTitle:[self countText:user.followersCount title:@"粉丝"] forState:UIControlStateNormal];
        
        //微博数量
        _status.text = [NSString stringWithFormat:@" 共%i条微博", user.statusesCount];
    }
}

#pragma mark - ASI代理

- (void)requestFinished:(ASIHTTPRequest *)request
{
    _icon.image = [UIImage imageWithData:[request responseData]];
}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在iOS,一个视图只能有一个UITableView。但是可以通过创建多个UITableView来实现一个视图显示多个表格的效果。以下是一个示例代码: 首先,你需要在视图控制器添加多个UITableView的实例变量: ```swift class YourViewController: UIViewController { var tableView1: UITableView! var tableView2: UITableView! // ... } ``` 然后,在视图加载完成后,你可以创建和配置这些UITableView的实例: ```swift override func viewDidLoad() { super.viewDidLoad() // 创建第一个UITableView tableView1 = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2)) tableView1.dataSource = self tableView1.delegate = self view.addSubview(tableView1) // 创建第二个UITableView tableView2 = UITableView(frame: CGRect(x: 0, y: view.frame.height/2, width: view.frame.width, height: view.frame.height/2)) tableView2.dataSource = self tableView2.delegate = self view.addSubview(tableView2) // ... } ``` 接下来,你需要实现UITableViewDataSource和UITableViewDelegate协议的相关方法来提供表格的数据和处理交互事件。例如: ```swift extension YourViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == tableView1 { // 返回第一个UITableView的行数 return 10 } else if tableView == tableView2 { // 返回第二个UITableView的行数 return 5 } return 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) if tableView == tableView1 { // 配置第一个UITableView的单元格 cell.textLabel?.text = "Table View 1 - Row \(indexPath.row)" } else if tableView == tableView2 { // 配置第二个UITableView的单元格 cell.textLabel?.text = "Table View 2 - Row \(indexPath.row)" } return cell } } extension YourViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView == tableView1 { // 处理第一个UITableView的行选事件 print("Table View 1 - Row \(indexPath.row) selected") } else if tableView == tableView2 { // 处理第二个UITableView的行选事件 print("Table View 2 - Row \(indexPath.row) selected") } } } ``` 这样,你就可以在同一个视图使用多个UITableView了。记得在视图控制器遵循UITableViewDataSource和UITableViewDelegate协议,并在视图加载完成后设置数据源和代理。 希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值