九宫格

来源:互联网 发布:js屏蔽手机运营商广告 编辑:程序博客网 时间:2024/04/29 20:08
#import
#import
@interface NineGridEntity : NSObject

@property (nonatomic, strong) NSString* id;
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) NSString* ImageName;
@property (nonatomic, strong) UIImage* icon;
@property (nonatomic, strong) NSString * unreadcount;
@property (nonatomic) int sortno;

@property (nonatomic, strong)UIImageView *iconIV;

@end

#import "NineGridEntity.h"

@implementation NineGridEntity

//@synthesizeid,name,icon,sortno,unreadcount,ImageName,iconIV;

@end

#import

@protocol NineGridTableDelegate

-(void)onClickNineGridTableButton:(NSString*)objid;

@end


@interface NineGridTable : UITableView

-(void)loadGridData:(NSMutableArray*)dataArray;

@property (nonatomic, assign)  id   nineGridTableDelegate;
@property (nonatomic, assign) int countinrow;
//设置表格单元格样式
+(void)setTableCellStyle:(UITableViewCell*)cell;

@end

#import "NineGridTable.h"
#import "NineGridEntity.h"
#import "UIImage+imageAlpha.h"

@interface NineGridTable(){
   NSMutableArray* dataArrays;
}

@end

@implementation NineGridTable
@synthesize nineGridTableDelegate;
@synthesize countinrow;

- (id)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self){
       // Initialization code
    }
   self.backgroundColor = [UIColor whiteColor];
   self.allowsSelection = FALSE;
   self.dataSource = self;
   self.delegate = self;
    [selfsetSeparatorColor:[UIColor clearColor]];
   self.countinrow = 3;
   
    returnself;
}


-(void)loadGridData:(NSMutableArray*)dataArray{
    dataArrays =[[NSMutableArray alloc] initWithArray:dataArray];
   
    [selfreloadData];
}


- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{
    returndataArrays.count % self.countinrow >0 ? dataArrays.count /self.countinrow + 1 : dataArrays.count / self.countinrow;
}

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString*ListViewCellId = @"NineGridCellId";
    CGFloatbuttonheight = tableView.frame.size.width / (self.countinrow +4.0f);
    CGFloatbuttonwidth = buttonheight;
   
   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ListViewCellId];
    if (cell ==nil) {
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ListViewCellId];
       
       CGFloat sidewidth = (tableView.frame.size.width -buttonheight*self.countinrow) / (self.countinrow + 1.0f);
       CGFloat sideheight = (tableView.frame.size.width / self.countinrow-buttonheight) / 2.0f;
       
       for(int i=0; i
           UIButton* button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
           button1.frame = CGRectMake(sidewidth * (i+1) + buttonwidth*i,sideheight, buttonwidth, buttonheight);
           button1.backgroundColor = [UIColor whiteColor];
           button1.tag = 1001 + i;
           [cell.contentView addSubview:button1];
           
           [button1 addTarget:self action:@selector(buttonClickImage:)forControlEvents:UIControlEventTouchUpInside];
       }
       
       for(int i=0; i
           UIButton* button1 = (UIButton*)[cell.contentViewviewWithTag:(1001+i)];
           
           UILabel* label1 = [[UILabel alloc]initWithFrame:CGRectMake(button1.frame.origin.x-15,button1.frame.origin.y+button1.frame.size.height,button1.frame.size.width+30, 20.0f)];
           label1.backgroundColor = [UIColor clearColor];
           label1.textColor = [UIColor blackColor];
           label1.font = [UIFont fontWithName:@"HelveticaNeue-Light"size:14];
           label1.tag = 2001 + i;
           label1.textAlignment = NSTextAlignmentCenter;
           [cell.contentView addSubview:label1];
       }

       CGFloat unreadheight = 19.0f;
       CGFloat cornerRadius = 10.0f;
       CGFloat offsetunread = 2.0f;
       CGFloat alphaunreawd = 0.8f;
       
       for(int i=0; i
           UIButton* button1 = (UIButton*)[cell.contentViewviewWithTag:(1001+i)];
           
           UILabel* labelunread1 = [[UILabel alloc]initWithFrame:CGRectMake(0.0f,0.0f,unreadheight,unreadheight)];
           labelunread1.center =CGPointMake(button1.frame.origin.x+button1.frame.size.width-offsetunread,button1.frame.origin.y+offsetunread);
           labelunread1.backgroundColor = [UIColor colorWithRed:1.0fgreen:0.0f blue:0.0f alpha:alphaunreawd];
           labelunread1.textColor = [UIColor whiteColor];
           labelunread1.font = [UIFont fontWithName:@"HelveticaNeue-Light"size:13];
           labelunread1.tag = 3001 + i;
           labelunread1.textAlignment = NSTextAlignmentCenter;
           [cell.contentView addSubview:labelunread1];
           [labelunread1.layer setCornerRadius:cornerRadius];
           [labelunread1.layer setMasksToBounds:YES];
       }
    }
   
   cell.backgroundColor = [UIColor clearColor];
   cell.contentView.tag = indexPath.row;
   
    for(int i=0;i
       UIButton* button1 = (UIButton*)[cell.contentViewviewWithTag:(1001+i)];
       UILabel* label1 = (UILabel*)[cell.contentViewviewWithTag:(2001+i)];
       UILabel* imgView1 = (UILabel*)[cell.contentViewviewWithTag:(3001+i)];
       
       button1.hidden = YES;
       label1.hidden = YES;
       imgView1.hidden = YES;
    }
   
    int index =0;
   for(NSInteger i= indexPath.row*(self.countinrow); (i<indexPath.row*(self.countinrow) + self.countinrow) &&i
       UIButton* button1 = (UIButton*)[cell.contentViewviewWithTag:(1001+index)];
       UILabel* label1 = (UILabel*)[cell.contentViewviewWithTag:(2001+index)];
       UILabel* imgView1 = (UILabel*)[cell.contentViewviewWithTag:(3001+index)];
       
       NineGridEntity* entity = dataArrays[i];

       button1.hidden = NO;
       label1.hidden = NO;
//找到保存图片的保存路径,并用文件处理单例找到图片的名称,查找本地解压过的图片文件对应的名称进行对九宫格图标的填充。如果没有文件则选择用本地保存好的默认的图片。

       NSString* homePath=NSHomeDirectory();
       NSString* BtnImageSetPath = [homePathstringByAppendingPathComponent:@"%@/ResoftR/ResoftRV3-3.02ForIos8/ResoftGPS/NineGridTable"];
       NSFileManager* file=[NSFileManager defaultManager];
       if ([file fileExistsAtPath:BtnImageSetPath])
       {
           
           [button1 setImage:[UIImage imageWithContentsOfFile:[BtnImageSetPathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.png",entity.ImageName]]]forState:UIControlStateNormal];
//
//           
//         [button1 setBackgroundImage:[UIImageimageWithContentsOfFile:[BtnImageSetPathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.png",entity.ImageName]]]forState:UIControlStateNormal];
//增加了九宫格界面的点击效果,如果已经下载了九宫格图片解压并填充成功后,则直接在在此填充方法中增加点击效果事件,当点击对应按钮手指弹起的时候显示效果。
//            测试点:
//            1、验证点击九宫格页面图片时,是否会有阴影效果。
//            2、验证点击后是否会跳转至响应的页面且页面标题与图标显示的标题一致
//            3、在测试过程中对有无卡死、闪退以及页面显示不全的状态进行验证
//             
//            */
//           [button1 setShowsTouchWhenHighlighted:YES];
       }
       else
       {
           [button1 setImage:entity.icon forState:UIControlStateNormal];
//           [button1 setBackgroundImage:entity.iconforState:UIControlStateNormal];
//           [button1 setBackgroundImage:entity.iconforState:UIControlStateHighlighted];
//           button1.backgroundColor = [UIColor redColor];
//
//           [button1 setShowsTouchWhenHighlighted:YES];
       }
       
      [button1 setImage:entity.icon forState:UIControlStateNormal];
       
//       [button1 setBackgroundImage:[UIImage sourceImage:entity.iconapplyingAlpha:1.0] forState:UIControlStateHighlighted];
       
       label1.text = entity.name;
       if ([entity.unreadcount integerValue] > 0) {
           imgView1.hidden = NO;
           imgView1.text = [NSString stringWithFormat:@"%@",entity.unreadcount];
           
           if ([entity.unreadcount integerValue] > 99) {
               
               int x = (int)[NSString stringWithFormat:@"%@",entity.unreadcount].length;
               NSMutableString* stlen = [[NSMutableString alloc] init];
               for (int i = 0; i < x; i++) {
                   [stlen appendString:@"9"];
               }
               if (stlen.length < 2) {
                   stlen = [[NSMutableString alloc] initWithString:@"99"];
               }
               CGFloat contentWidth = [stlenboundingRectWithSize:CGSizeMake(CGFLOAT_MAX,imgView1.frame.size.height)options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:13],NSFontAttributeName, nil]context:nil].size.width;
               
               imgView1.frame =CGRectMake(imgView1.frame.origin.x-(contentWidth-imgView1.frame.size.width),imgView1.frame.origin.y,contentWidth,imgView1.frame.size.height);
           } else {
               CGFloat offsetunread = 2.0f;
               imgView1.frame =CGRectMake(0,0,imgView1.frame.size.height,imgView1.frame.size.height);
               imgView1.center =CGPointMake(button1.frame.origin.x+button1.frame.size.width-offsetunread,button1.frame.origin.y+offsetunread);
           }
       }
       else{
           imgView1.hidden = YES;
       }
       
       index ++;
    }
   
   [NineGridTable setTableCellStyle:cell];
    returncell;
}
//设置表格单元格样式
+(void)setTableCellStyle:(UITableViewCell*)cell{
    if ([cellrespondsToSelector:@selector(setSeparatorInset:)]) {
       [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cellrespondsToSelector:@selector(setLayoutMargins:)]) {
       [cell setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}


-(void)buttonClickImage:(UIButton*)button{
    NSIntegerindex = (button.tag - 1001) +button.superview.tag*(self.countinrow);
   NineGridEntity* entity = dataArrays[index];

    if(self.nineGridTableDelegate && [self.nineGridTableDelegaterespondsToSelector:@selector(onClickNineGridTableButton:)]){
       [nineGridTableDelegateonClickNineGridTableButton:entity.name];
    
    }
}

- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{
    returntableView.frame.size.width / (self.countinrow);
}

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableViewdeselectRowAtIndexPath:indexPath animated:NO];
}



@end
注意:要引入图片的路径,2.就是导入SDWebImage框架.
0 0