您的位置:首页 > 产品设计 > UI/UE

iOS 自定义UITableViewCell

2015-12-31 16:46 405 查看
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


#import "AppDelegate.h"
#import "AViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

AViewController *root = [AViewController new];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:root];

self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}

@end


#import <UIKit/UIKit.h>

@interface AViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
// 呈现数据源的控件
UITableView *_tableView;

// 数据源
NSArray *datas;
}
@end


#import "AViewController.h"
#import "CumstomCell.h"//导入自定义的cell

@implementation AViewController

-(void)loadView
{
[super loadView];

// 不分组的plain样式
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStylePlain];
// 数据源代理
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}

- (void)viewDidLoad {
[super viewDidLoad];

NSString *str = @"A,B,C,D,E,F,G,H,I,J,K,L,M,N";
datas = [str componentsSeparatedByString:@","];
}

#pragma mark -数据源配置-
// 配置tablview显示多少行数据
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return datas.count;
}

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

// 每个cell显示什么内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *indentity = @"cell";
//使用自定义的cell
CumstomCell *cell = [_tableView dequeueReusableCellWithIdentifier:indentity];
if (cell == nil) {
cell=[[CumstomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity];
}

//NSLog(@"%d",indexPath.row);
//NSString *t = [datas objectAtIndex:indexPath.row];

//cell.textLabel.text = t;
cell.imgView1.image = [UIImage imageNamed:@"123.jpg"];
cell.imgView2.image = [UIImage imageNamed:@"123.jpg"];
cell.imgView3.image = [UIImage imageNamed:@"123.jpg"];

return cell;
}

@end


#import <UIKit/UIKit.h>

@interface CumstomCell : UITableViewCell

@property(nonatomic,strong)UIImageView *imgView1;
@property(nonatomic,strong)UIImageView *imgView2;
@property(nonatomic,strong)UIImageView *imgView3;

@end


#import "CumstomCell.h"

@implementation CumstomCell
@synthesize imgView1=_imgView1;
@synthesize imgView2=_imgView2;
@synthesize imgView3=_imgView3;

-(void)dealloc{

/*
[_imgView1 release];
[_imgView2 release];
[_imgView3 release];
[super dealloc];
*/

_imgView1 = nil;
_imgView2 = nil;
_imgView3 = nil;
}
/**
*  重写该方法
*/
-(UITableViewCell *)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

self.imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 100)];
//self.imgView1.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.imgView1];

self.imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(10+80+10, 10, 80, 100)];
//self.imgView2.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.imgView2];

self.imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(10+2*80+2*10, 10, 80, 100)];
//self.imgView3.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.imgView3];

}

return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: