qq列表代码

来源:互联网 发布:安徽数据堂 编辑:程序博客网 时间:2024/05/16 10:50
import <UIKit/UIKit.h>


@interface HRTableCell : UITableViewCell
{




    UIView *contentView;




}
- (void)drawContentView:(CGRect)rect;

@end



#import "HRTableCell.h"






@interface HRTableCellView : UIView




@end






@implementation HRTableCellView






- (void)drawRect:(CGRect)rect
{
  


  [(HRTableCell *)[self superview] drawContentView:rect];




}




@end






@implementation HRTableCell






- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  


  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
      


  contentView = [[HRTableCellView alloc] init];




        contentView.opaque = YES;
   


     [self addSubview:contentView];
    }
  


  return self;
}






- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
   


 CGRect b = self.bounds;
   


 b.size.height -= 1.0f;
  


  contentView.frame = b;
}






- (void)setNeedsDisplay
{
    [super setNeedsDisplay];
  


  [contentView setNeedsDisplay];
}






- (void)drawContentView:(CGRect)rect
{
    
}


@end

#import "HRTableCell.h"






@interface HRFriendsCell : HRTableCell




@property (weak, nonatomic) NSString *name;




@property (assign, nonatomic, getter = isOnline) BOOL online;




@property (weak, nonatomic) UIImage *headerImage;




- (void)bindFriend:(NSDictionary *)myFriend;
@end

#import "HRFriendsCell.h"






#define RTitleSize 16.0f




#define RDetailSize 12.0f






static UIFont * TextFont = nil;




@implementation HRFriendsCell






+ (void)initialize
{
    TextFont = [UIFont fontWithName:@"American Typewriter" size:RTitleSize];
}






- (void)bindFriend:(NSDictionary *)myFriend
{
 


   _name = myFriend[@"name"];
  


  _online = [myFriend[@"isonline"] boolValue];
 


   _headerImage = [UIImage imageNamed:myFriend[@"imagename"]];




   [self setNeedsDisplay];
}






- (void)drawContentView:(CGRect)rect
{
 


   UIColor *titleColor;
 


   titleColor = [UIColor blackColor];
  


  UIColor *detailColor;
  


  detailColor = [UIColor lightGrayColor];
    




  CGContextRef context = UIGraphicsGetCurrentContext();
 


   CGSize size = self.frame.size;
  


  if (self.selected || self.highlighted)
    {
 


       CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0].CGColor);




     CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    }
   


 else
    {
     


   CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
   


     CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    }


  


  [_headerImage drawInRect:CGRectMake(5, 5, 40, 40)];
 


   
    [titleColor set];
  


  [_name drawAtPoint:CGPointMake(50, 5)
   forWidth:200
              withFont:TextFont
 fontSize:RTitleSize
         lineBreakMode:NSLineBreakByTruncatingTail
    baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
 


   
    [detailColor set];
 


   NSString *detailString;
 


   if (self.isOnline)
    {
  


      detailString = @"[在线]";
    }
  


  else
    {
    


    detailString = @"[不在线]";
    }
 


   [detailString drawAtPoint:CGPointMake(50, 28)
                     forWidth:100
                     withFont:TextFont
                     fontSize:RDetailSize
                lineBreakMode:NSLineBreakByTruncatingTail
     


      baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}






@end



#import "HeaderButton.h"






#define RMarginX 5




#define RMarginY 15




#define RIconSide 20




#define ROffsetY 5.0f






@implementation HeaderButton






- (id)initWithFrame:(CGRect)frame
{
   


 self = [super initWithFrame:frame];




    if (self) {
      


  UIImage *image = [UIImage imageNamed:@"arrow-right"];
  


      [self setImage:image forState:UIControlStateNormal];




        self.imageView.contentMode = UIViewContentModeScaleAspectFit;




        //[self setTitleColor:[UIColor blackColor] 


forState:UIControlStateNormal];
        _open = NO;
    }
   


 return self;
}






- (void)setOpen:(BOOL)open
{
 


   _open = open;
    //设定点击旋转动画效果
  


  [UIView beginAnimations:nil context:nil];
 


   self.imageView.transform = CGAffineTransformMakeRotation(self.isOpen?M_PI_2:0);
  


  [UIView commitAnimations];
}






- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSaveGState(context);
    CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor);
    CGContextMoveToPoint(context, 0, rect.size.height);
    CGContextAddLineToPoint(context, 320, rect.size.height);
    CGContextSetLineWidth(context, 2.0f);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    UIColor *light = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
    UIColor *dark = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35];
    NSArray *colors = @[(__bridge id)light.CGColor, (__bridge id)dark.CGColor];
    CGFloat locations[] = {0.0, 1.0};
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
    
    CGContextSaveGState(context);
    CGPoint start = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint end = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGContextDrawLinearGradient(context, gradient, start, end, 0);
    
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    
//    CGColorSpaceRef offColorSpace = CGColorSpaceCreateDeviceRGB();
//    UIColor *lightOffset = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35];
//    UIColor *darkOffset = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
//    NSArray *offsetColors = @[(__bridge id)lightOffset.CGColor, (__bridge id)darkOffset.CGColor];
//    CGFloat offsetLoactions[] = {0.0, 1.0};
//    
//    CGGradientRef offsetGradient = CGGradientCreateWithColors(offColorSpace, (__bridge CFArrayRef)offsetColors, offsetLoactions);
//    CGContextSaveGState(context);
//    CGPoint offStart = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
//    CGPoint offEnd = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)+ROffsetY*2);
//    CGContextDrawLinearGradient(context, offsetGradient, offStart, offEnd, 0);
//    
//    CGGradientRelease(offsetGradient);
//    CGColorSpaceRelease(colorSpace);
//    CGContextRestoreGState(context);
}


//图像显示位置




- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
  


  return CGRectMake(RMarginX, RMarginY, RIconSide, RIconSide);
}


//标题显示位置




- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
  


  return CGRectMake(RIconSide + 4*RMarginX, 0, contentRect.size.width, contentRect.size.height);
}


@end



#import "ViewController.h"




#import "HRFriendsCell.h"




#import "HeaderButton.h"






#define RTagOffset 10




#define RRowHeight 50.0f




#define RHeaderHeight 45.0f






@interface ViewController ()
{
  


  NSArray *_dataList;
   


 NSMutableArray *_groupNames;
   


 
    NSMutableDictionary *_headers;
}
@end






static NSString * const CellIdentifier = @"FriendCell";




@implementation ViewController






- (void)viewDidLoad
{
  


  [super viewDidLoad];
  


  [self loadData];
   


 self.title = @"好友分组";
   


 [self.tableView registerClass:[HRFriendsCell class] 
forCellReuseIdentifier:CellIdentifier];
}






- (void)loadData
{
   


 NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@"plist"];
  


  _dataList = [NSArray arrayWithContentsOfFile:path];
 


   _headers = [NSMutableDictionary dictionaryWithCapacity:_dataList.count];
 


   
    _groupNames = [NSMutableArray arrayWithCapacity:_dataList.count];
 


   for (NSInteger i = 0; i < _dataList.count; i++)
    {
 


       NSDictionary *dict = _dataList[i];
   


     [_groupNames addObject:dict[@"groupname"]];
    }
}






- (NSArray *)arrayWithSection:(NSInteger)number
{
 


   NSDictionary *dict = _dataList[number];
 


   NSArray *friendsArray = dict[@"list"];
   


 
    return friendsArray;
}






- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 


   return _groupNames.count;
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  


  HeaderButton *header = _headers[@(section)];
 


   NSArray *array = [self arrayWithSection:section];
  


  NSInteger count = header.isOpen?array.count:0;
 


   return count;
}






- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 


   HRFriendsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  


  
    NSArray *array = [self arrayWithSection:indexPath.section];




    [cell bindFriend:array[indexPath.row]];




    
    return cell;
}






- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
 


   HeaderButton *header = _headers[@(section)];
 


   if (!header)
    {
    


    header = [HeaderButton buttonWithType:UIButtonTypeCustom];




        header.bounds = CGRectMake(0, 0, 320, RHeaderHeight);
 


       header.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.8 alpha:1.0];
   


     header.titleLabel.font = [UIFont systemFontOfSize:16.0f];
 


       NSString *title = _groupNames[section];
   


     [header setTitle:title forState:UIControlStateNormal];
 


       [header addTarget:self action:@selector(expandFriends:) 
forControlEvents:UIControlEventTouchUpInside];
 


       [_headers setObject:header forKey:@(section)];
    }
 


   return header;
}






- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
 


   return RHeaderHeight;
}






- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return RRowHeight;
}






- (void)expandFriends:(HeaderButton *)header
{
 


   header.open = !header.isOpen;
 


   [self.tableView reloadData];
}


@end

原创粉丝点击