九宫格

#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

//@synthesize id,name,icon,sortno,unreadcount,ImageName,iconIV;

@end

#import

@protocol NineGridTableDelegate

-(void)onClickNineGridTableButt on:(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;
      [self setSeparatorColor:[UIColor clearColor]];
      self.countinrow = 3;
     
      return self;
}


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


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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      NSString *ListViewCellId = @"NineGridCellId";
      CGFloat buttonheight = tableView.frame.size.width / (self.countinrow + 4.0f);
      CGFloat buttonwidth = buttonheight;
     
      UITableViewCell *cell = [tableView dequeueReusableCellWithI dentifier:ListViewCellId];
      if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefa ult reuseIdentifier: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 = [UIButton buttonWithType: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:UIControlEventTouchUpIns ide];
              }
             
              for(int i=0; i
                      UIButton* button1 = (UIButton*)[cell.contentView viewWithTag:(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.contentView viewWithTag:(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.0f green: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.contentView viewWithTag:(1001+i)];
              UILabel* label1 = (UILabel*)[cell.contentView viewWithTag:(2001+i)];
              UILabel* imgView1 = (UILabel*)[cell.contentView viewWithTag:(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.contentView viewWithTag:(1001+index)];
              UILabel* label1 = (UILabel*)[cell.contentView viewWithTag:(2001+index)];
              UILabel* imgView1 = (UILabel*)[cell.contentView viewWithTag:(3001+index)];
             
              NineGridEntity* entity = dataArrays[i];

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

              NSString* homePath=NSHomeDirectory();
              NSString* BtnImageSetPath = [homePath stringByAppendingPathCom ponent:@"%@/ResoftR/ResoftRV3-3.02ForIos8/ResoftGPS/NineGridTable"];
              NSFileManager* file=[NSFileManager defaultManager];
              if ([file fileExistsAtPath:BtnImageSetPath])
              {
                     
                      [button1 setImage:[UIImage imageWithContentsOfFile:[BtnImageSetPath stringByAppendingPathCom ponent:[NSString stringWithFormat:@"%@.png",entity.ImageName]]] forState:UIControlStateNormal];
//
//                     
//                  [button1 setBackgroundImage:[UIImage imageWithContentsOfFile:[BtnImageSetPath stringByAppendingPathCom ponent:[NSString stringWithFormat:@"%@.png",entity.ImageName]]] forState:UIControlStateNormal];
//增加了九宫格界面的点击效果,如果已经下载了九宫格图片解压并填充成功后,则直接在在此填充方法中增加点击效果事件,当点击对应按钮手指弹起的时候显示效果。
//                        测试点:
//                        1、验证点击九宫格页面图片时,是否会有阴影效果。
//                        2、验证点击后是否会跳转至响应的页面且页面标题与图标显示的标题一致
//                        3、在测试过程中对有无卡死、闪退以及页面显示不全的状态进行验证
//                         
//                        */
//                      [button1 setShowsTouchWhenHighlig hted:YES];
              }
              else
              {
                      [button1 setImage:entity.icon forState:UIControlStateNormal];
//                      [button1 setBackgroundImage:entity.icon forState:UIControlStateNormal];
//                      [button1 setBackgroundImage:entity.icon forState:UIControlStateHighlighte d];
//                      button1.backgroundColor = [UIColor redColor];
//
//                      [button1 setShowsTouchWhenHighlig hted:YES];
              }
             
            [button1 setImage:entity.icon forState:UIControlStateNormal];
             
//              [button1 setBackgroundImage:[UIImage sourceImage:entity.icon applyingAlpha:1.0] forState:UIControlStateHighlighte d];
             
              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 = [stlen boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, imgView1.frame.size.height) options:NSStringDrawingUsesLineF ragmentOrigin|NSStringDrawingUsesFontL eading attributes:[NSDictionary dictionaryWithObjectsAnd Keys:[UIFont systemFontOfSize: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];
      return cell;
}
//设置表格单元格样式
+(void)setTableCellStyle:(UITableViewCell*)cell{
      if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
              [cell setSeparatorInset:UIEdgeInsetsZero];
      }
      if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
              [cell setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
      }
}


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

      if (self.nineGridTableDelegate && [self.nineGridTableDelegate respondsToSelector:@selector(onClickNineGridTableButt on:)]){
              [nineGridTableDelegate onClickNineGridTableButt on:entity.name];
       
      }
}

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

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



@end
注意:要引入图片的路径,2.就是导入SDWebImage框架.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值