IOS单例模式

来源:互联网 发布:评价算法性能的标准 编辑:程序博客网 时间:2024/06/05 12:42
#import "ShareInstanceViewController.h"
#import "HeadFile.h"



@interface ShareInstanceViewController ()

@end

@implementation ShareInstanceViewController


static ShareInstanceViewController *sharedObj = nil;


+ (ShareInstanceViewController *) sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedObj=[[ShareInstanceViewController alloc] init];
        
        
    });
    
    return sharedObj;
}

+ (id) allocWithZone:(NSZone *)zone //第三步:重写allocWithZone方法
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedObj=[super allocWithZone:zone];
        
        
    });
    
    return sharedObj;
}
- (id) copyWithZone:(NSZone *)zone //第四步
{
    return self;
}

- (id) retain
{
    return self;
}

- (unsigned) retainCount
{
    return UINT_MAX;
}

- (oneway void) release
{
    
}

- (id) autorelease
{
    return self;
}
- (id)init
{
    @synchronized(self) {
        
        [super init];//往往放一些要初始化的变量.
        
        
        return self;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416+(isIPhone5?88:0)) style:UITableViewStyleGrouped];
    table.backgroundView =nil;
    //    table.backgroundColor = [UIColor clearColor];
    //    table.backgroundColor=TABLE_BACKGROUDN_COLOR;
    table.delegate=self;
    table.dataSource=self;
    table.showsVerticalScrollIndicator=NO;
    table.separatorColor=TABLE_SEPARATOR_COLOR;
    table.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  
    
    [self.view addSubview:table];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    
    
    
    return 100;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return     44;
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
    }
    
    
    //    cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
    //    cell.selectedBackgroundView.backgroundColor = CELL_BACKGROUDN_COLOR;
    
    
    
    
    
    UILabel *label=[[UILabel alloc] init];
    label.frame=CGRectMake(54, 0, 200, 44);
    label.backgroundColor=[UIColor clearColor];
    label.text=[NSString stringWithFormat:@"----%d",indexPath.row];
    [cell addSubview:label];
    [label release];
    
    
    
    
    
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
  
    
    
    
    
}
0 0
原创粉丝点击