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

来源:互联网 发布:mac照片如何归类 编辑:程序博客网 时间:2024/04/30 07:18

- (instancetype)initWithCourse:(TTCourse *)aCourse type:(TTCourseCellType)aType

{

    self = [superinit];//这里是必须要写的对于重写init方法来说,开头就是这么写的不能忘记。

    if (self !=nil)

    {

        self.course = aCourse;

        self.clipsToBounds =YES;

        

        //封面图

        self.coverImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(15, 15, 60  , 60)];

        [self.coverImageViewsd_setImageWithURL:[NSURLURLWithString:self.course.coverURL]placeholderImage:[UIImageimageNamed:@"album_holder"]];

        /**

         *  圆角从2改成3

         */

        self.coverImageView.layer.cornerRadius = 3.0f;

        [self.contentViewaddSubview:self.coverImageView];

        

        //曲目名字

        self.titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(90, 17, 200, 21)];

        self.titleLabel.text =self.course.title;

        self.titleLabel.font =TTCommonFont(20);

        /**

         *  设置字体的风格

         */

        

        [self.contentViewaddSubview:self.titleLabel];


        //作者

        self.authorLabel = [[UILabelalloc] initWithFrame:CGRectMake(90, 44, 200, 15)];

        self.authorLabel.text =self.course.artist;

        self.authorLabel.font =TTCommonFont(12);

        [self.contentViewaddSubview:self.authorLabel];

        

        //难度

        self.difficultyLabel = [[UILabelalloc] initWithFrame:CGRectMake(140, 100, 200, 20)];

        self.difficultyLabel.font =TTCommonFont(14);

        CGSize textSize = [self.difficultyLabel.textsizeWithAttributes:@{NSFontAttributeName   :self.difficultyLabel.font}];

        /**

         *  72

         */

        for (int i = 0; i < 5; i++)

        {

            UIImage* image = i <self.course.difficulty.intValue ? [UIImageimageNamed:@"icon-difficulty-active@3x.png"] : [UIImageimageNamed:@"icon-difficulty-default@3x.png"];

            UIImageView* iv = [[UIImageViewalloc] initWithImage:image];

            CGPoint center =self.difficultyLabel.center;

            center.x += (textSize.width / 2 + i * 10)-146;

            center.y=72.0;

            iv.center = center;

            [self.contentViewaddSubview:iv];

        }

        

        switch (aType) {

            caseECourseTypePractice:

                // 加入进度标识

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

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

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

                self.progressView.hidden =YES;

                [self.contentViewaddSubview:self.progressView];

                break;

            caseECourseTypeFavorite:

                // 加入下载按钮

                self.downloadButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

                [self.downloadButtonsetImage:[UIImageimageNamed:@"me_download_button"]forState:UIControlStateNormal];

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

                [self.downloadButtonaddTarget:self

                                       action:@selector(downloadAction:)

                             forControlEvents:UIControlEventTouchUpInside];

                self.downloadButton.hidden =YES;

                [self.contentViewaddSubview:self.downloadButton];

                break;

            caseECourseTypeCommentAndLike:

                // 加入点赞和评论

                

                break;

            default:

                break;

        }

    }

    returnself;

}

这是对于自己的一个tableviewcell的自定义,这个重新写了init的方法,可以返回自己想要的产生的类型。

至于和id的区别还没有研究。id好像最终不能返回你想要的那个类。。。后续会补充

0 0
原创粉丝点击