iphone开发之自定义TableViewCell

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell
{
    UIImageView *imageView;

}

-(void)setTheImage:(UIImage *)icon;


@end

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell


#pragma mark---------setTheImage------

-(void)setTheImage:(UIImage *)icon{

    imageView = [[UIImageView alloc]initWithImage:icon];
    imageView.frame = CGRectMake(0, 0, 320, 45);
    [self.contentView addSubview:imageView];
    

}

#pragma  mark ------去除cell的背景色

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    
    if (self== [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //cell background kill
        
        [self.contentView setBackgroundColor:[UIColor clearColor]];
        
    }

    
    return self;

}

#pragma  mark ------调整cell选中与未选中的透明度
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{

    [super setSelected:selected animated:animated];
    
    if (selected == YES) {
        //
        imageView.alpha =.5;
        
    }else{
    
        imageView.alpha =1;
    }

}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

CategoryViewController.h

#import <UIKit/UIKit.h>

@interface CategoryViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *table;
    NSMutableArray *muArr;
    

}


@end

CategoryViewController.m

#import "CategoryViewController.h"
#import "CustomCell.h"
@implementation CategoryViewController

#pragma mark -------UITableViewDelegate method(行高):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 45;

}

#pragma mark ---点击某行触发的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // *nextViewController = [[<#NextViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // Configure the new view controller.

  //  [self.navigationController pushViewController:nextViewController animated:YES];
   // [nextViewController release];
    
    
    
    
}

#pragma mark UITableViewDataSource method:



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }

    [cell setTheImage:[UIImage imageNamed:@"条纹.png"]];
    
    //
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 300, 40)];
    lbl.backgroundColor  = [UIColor clearColor];
    lbl.tag = indexPath.row;
    lbl.textColor = [UIColor blackColor];
    lbl.text = @"第一章:秦始皇统一六国概述";//arry indexPath.row
    [cell addSubview:lbl];
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 9;//array count
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
    return 1;

}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

#pragma mark - 在viewWillAppear去掉navigationBar(顶部的导航)
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; self.navigationController.navigationBar.hidden = YES;}// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad{ [super viewDidLoad]; self.navigationController.navigationBar.hidden = YES; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 436)]; imageView.image = [UIImage imageNamed:@"Default.png"]; [self.view addSubview:imageView]; [imageView release]; table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 436) style:UITableViewStylePlain]; table.scrollEnabled = YES; table.delegate = self; table.dataSource = self; table.backgroundColor = [UIColor clearColor]; table.separatorStyle = UITableViewCellSeparatorStyleNone;// 去掉cell的线 table.indicatorStyle = UIScrollViewIndicatorStyleWhite; [self.view addSubview:table]; }- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值