您的位置:首页 > 移动开发 > IOS开发

自己对于instancetype的理解和应用iOS开发的过程

2015-11-23 00:55 357 查看
- (instancetype)initWithCourse:(TTCourse *)aCourse type:(TTCourseCellType)aType
{
self = [super
init];//这里是必须要写的对于重写init方法来说,开头就是这么写的不能忘记。
if (self !=
nil)
{
self.course = aCourse;
self.clipsToBounds =
YES;

//
封面图
self.coverImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(15, 15, 60 , 60)];
[self.coverImageView
sd_setImageWithURL:[NSURL
URLWithString:self.course.coverURL]
placeholderImage:[UIImage
imageNamed:@"album_holder"]];
/**
* 圆角从2改成3
*/
self.coverImageView.layer.cornerRadius
= 3.0f;
[self.contentView
addSubview:self.coverImageView];

//
曲目名字
self.titleLabel = [[UILabel
alloc] initWithFrame:CGRectMake(90, 17, 200, 21)];
self.titleLabel.text =
self.course.title;
self.titleLabel.font =
TTCommonFont(20);
/**
* 设置字体的风格
*/

[self.contentView
addSubview:self.titleLabel];

//
作者
self.authorLabel = [[UILabel
alloc] initWithFrame:CGRectMake(90, 44, 200, 15)];
self.authorLabel.text =
self.course.artist;
self.authorLabel.font =
TTCommonFont(12);
[self.contentView
addSubview:self.authorLabel];

//
难度
self.difficultyLabel = [[UILabel
alloc] initWithFrame:CGRectMake(140, 100, 200, 20)];
self.difficultyLabel.font =
TTCommonFont(14);
CGSize textSize = [self.difficultyLabel.text
sizeWithAttributes:@{NSFontAttributeName :
self.difficultyLabel.font}];
/**
* 72
*/
for (int i = 0; i < 5; i++)
{
UIImage* image = i <
self.course.difficulty.intValue ? [UIImage
imageNamed:@"icon-difficulty-active@3x.png"] : [UIImage
imageNamed:@"icon-difficulty-default@3x.png"];
UIImageView* iv = [[UIImageView
alloc] initWithImage:image];
CGPoint center =
self.difficultyLabel.center;
center.x += (textSize.width / 2 + i * 10)-146;
center.y=72.0;
iv.center = center;
[self.contentView
addSubview:iv];
}

switch (aType) {
case
ECourseTypePractice:

// 加入进度标识

self.progressView = [[MLVCircularProgressView
alloc] initWithFrame:CGRectMake(SYSTEM_SCREEN_WIDTH - 44, 30, 25,
25)];

self.progressView.center =
self.downloadButton.center;

self.progressView.shapeColor = [UIColor
colorWithHexString:@"#cbbcad"];

self.progressView.hidden =
YES;
[self.contentView
addSubview:self.progressView];

break;
case
ECourseTypeFavorite:

// 加入下载按钮

self.downloadButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[self.downloadButton
setImage:[UIImage
imageNamed:@"me_download_button"]
forState:UIControlStateNormal];

self.downloadButton.frame =
CGRectMake(SYSTEM_SCREEN_WIDTH - 44, 30, 44, 44);
[self.downloadButton
addTarget:self

action:@selector(downloadAction:)

forControlEvents:UIControlEventTouchUpInside];

self.downloadButton.hidden =
YES;
[self.contentView
addSubview:self.downloadButton];

break;
case
ECourseTypeCommentAndLike:

// 加入点赞和评论

break;
default:

break;
}
}
return
self;
}
这是对于自己的一个tableviewcell的自定义,这个重新写了init的方法,可以返回自己想要的产生的类型。
至于和id的区别还没有研究。id好像最终不能返回你想要的那个类。。。后续会补充
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: