(material source) Cat learn iOS (17) UI Pure code custom cell implementation Sina Weibo UI

Source: Internet
Author: User

Cat Share, must boutique

Material code Address: http://download.csdn.net/detail/u013357243/8580249
Original address: Http://blog.csdn.net/u013357243?viewmode=contents

Look first.

Programming ideas code to create a cell

1> Create custom cell, inherit from UITableViewCell
2> determine the control as required, and define the properties
3> Use getter method to complete the instantiation of the control, only create and add to Contentview, do not handle the location
4> defines a model property that, through setter methods, sets the cell's display

Location algorithm for nickname body string
The set size is determined by the length of the text using the string method: [@""Boundingrectwithsize: (cgsizeOptions: (nsstringdrawingoptions) Attributes: (nsdictionary*) Context: (Nsstringdrawingcontext *)]//boundingrectwithsize calculates the area occupied by a given literal string, and returns an X, Y, 0 cgrect//If you want to calculate the exact height of multiple rows requires incoming//Options:nsstringdrawinguseslinefragmentorigin//attribbutes:dict A dictionary of related properties for the specified font. Uikit Frame First header file PS This head file is hard to find.//Context:nil#define Knamefont [Uifont systemfontofsize:14]    nsdictionary*namedict = @{nsfontattributename:knamefont};CGRectNameframe = [ Self. Status. NameBoundingrectwithsize:cgsizemake (Maxfloat, maxfloat) Options:nsstringdrawinguseslinefragmentorigin attributes: Namedict Context:Nil];
Methods for calculating Row heights

To use the Proxy method:

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

The method of calculating the row height, how many rows are calculated when the table data is loaded contentsize

 问题:此方法执行的时候,cell还没有被实例化! 但是:行高计算是在实例化cell时,通过设置status属性,计算=》有了status模型,就可以知道行高  ! 问题:如何在cell实例化之前,获得行高? 解决方法:通过status可以计算得到行高! = 》再建立一个模型,专门计算所有控件的位置
Warning: The original cell must be resolved with another reusable identifier

Warning: file:///Users/apple/Desktop/%E5%AD%A6%E4%B9%A0/%E4%BA%8C%E6%9C%9F%E5%AD%A6%E4%B9%A0/Day07/%E6%96%B0%E6%B5% aa%e5%be%ae%e5%8d%9aui/%e6%96%b0%e6%b5%aa%e5%be%ae%e5%8d%9aui/base.lproj/main.storyboard:warning:unsupported Configuration:prototype table cells must have reuse identifiers
Warning: A prototype cell must have another reusable identifier
The workaround is to add a reusable identifier to the Identfier in the cell

Then be sure to associate the cell's class

--These two can be replaced by one line of code.

//为tableView注册可重用单元格    [selfclass] forCellReuseIdentifier:ID];

This time we comment

//    if (cell == nil) {//        cell = [[NYStatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];//    }

You can also run the

    在Storyboard中指定了可重用标示符,同时指定了Cell的类是NYStatusCell,系统会为tableView注册一个原形cell,专门用来做可重用单元格,一旦缓冲区不存在可重用单元格,系统会使用原形Cell新实例化一个Cell供程序使用!因此如果在Storyb中,注册了原形Cell,就不需要做 cell == nil 的判断了

Note: These are only available after iOS6.

Code Learning Custom Cell Pure code write Nystatuscell
//ps: New iOS Exchange Learning Group: 304570962Can add the cat cat QQ:1764541256Or Znycat Let's study hard together. Original: http://blog.csdn.net/u013357243?viewmode=contents//NYSTATUSCELL.M//Sina Weibo UI////Created by Apple on 15-4-9.//Copyright (c) 2015 Znycat. All rights reserved.//#import "NYStatusCell.h" #import "NYStatus.h" #import "NYStatusFrame.h" /** Name font * /#define Knamefont [Uifont systemfontofsize:14]/** Body font * /#define Ktextfont [Uifont systemfontofsize:16] @interface nystatuscell()//1> Create custom Iyicell, inherit from UITableViewCell//2> determines the control, depending on the requirements, and defines the properties. @property(nonatomic,Strong)Uiimageview*iconview;@property(nonatomic,Strong)UILabel*nameview;@property(nonatomic,Strong)Uiimageview*vipview;@property(nonatomic,Strong)UILabel*textview;@property(nonatomic,Strong)Uiimageview*pictureview;@end @implementation Nystatuscell //3> uses the Get method to complete the instantiation of the control, only to create and add to Contentview, not to handle the location. -(Uiimageview*) iconview{if(_iconview = =Nil) {_iconview = [[UiimageviewALLOC] init]; [ Self. ContentviewAddsubview:_iconview]; }return_iconview;} -(UILabel*) nameview{if(_nameview = =Nil) {_nameview = [[UILabelALLOC] init];//default font is number 17th, change to Knamefont_nameview. Font= Knamefont; [ Self. ContentviewAddsubview:_nameview]; }return_nameview;} -(Uiimageview*) vipview{if(_vipview = =Nil) {_vipview = [[UiimageviewALLOC] init]; [ Self. ContentviewAddsubview:_vipview]; }return_vipview;} -(UILabel*) textview{if(_textview = =Nil) {_textview = [[UILabelALLOC] init]; _textview. Font= Ktextfont; _textview. NumberOfLines=0;//Let him have a break.[ Self. ContentviewAddsubview:_textview]; }return_textview;} -(Uiimageview*) pictureview{if(_pictureview = =Nil) {_pictureview = [[UiimageviewALLOC] init]; [ Self. ContentviewAddsubview:_pictureview]; }return_pictureview;} -(void) Setstatusframe: (Nystatusframe *) statusframe{_statusframe = Statusframe;//1> setting up data[ SelfSettingdata];//2> Setting Location[ SelfSettingframe];}/** Setting Data * /-(void) settingdata{Nystatus *status = Self. Statusframe. Status;//Avatar     Self. IconView. Image= [UIImageImagenamed:status. Icon];//Name     Self. Nameview. Text= Status. Name;//VIP    if(Status. VIP) { Self. Vipview. Image= [UIImageimagenamed:@"VIP"]; }//Content body     Self. TextView. Text= Status. Text;//Picture Optional parameters:    if(Status. Picture. Length>0) { Self. Pictureview. Hidden=YES; Self. Pictureview. Image= [UIImageImagenamed:status. Picture]; } Self. Pictureview. Hidden=NO;}/** Setting Location * /-(void) settingframe{//1. Avatar     Self. IconView. Frame= Self. Statusframe. iconf;//2, the name size is determined by the length of the text//boundingrectwithsize calculates the area occupied by a given literal string, and returns an X, Y, 0 cgrect;w,h is a calculated width and height//If you want to calculate the exact height of multiple rows requires incoming//Options:nsstringdrawinguseslinefragmentorigin//attribbutes:dict A dictionary of related properties for the specified font. Uikit Frame First header file PS This head file is hard to find.//Context:nil     Self. Nameview. Frame= Self. Statusframe. NAMEF;//3,VIP Pictures     Self. Vipview. Frame= Self. Statusframe. Vipf;//4, Text     Self. TextView. Frame= Self. Statusframe. TEXTF;//5, Photo     Self. Pictureview. Frame= Self. Statusframe. Picturef;}@end
Class structure

This small project consists mainly of these classes

MVC is responsible for each individual thing.
Model has two models, one is the status of the main responsible for all data
The status has

@property (nonatomic, copy)NSString *text;@property (nonatomic, copy)NSString *icon;@property (nonatomic, copy)NSString *name;@property (nonatomicassign)BOOL vip;@property (nonatomicNSString *picture;

These properties mainly include Avatar, nickname, VIP icon, body, picture
The Statusframe model is primarily responsible for storing the location of each component in the cell

//Improve safety performance: +readonly@property(nonatomic,Assign,ReadOnly)CGRectTEXTF;@property(nonatomic,Assign,ReadOnly)CGRecticonf;@property(nonatomic,Assign,ReadOnly)CGRectNAMEF;@property(nonatomic,Assign,ReadOnly)CGRectVIPF;@property(nonatomic,Assign,ReadOnly)CGRectPicturef;/** Line Height * /@property(nonatomic,Assign)CGFloatCellheight;/** the size of all controls can be calculated by status * /@property(nonatomic,Strong) Nystatus *status;

PS: New iOS Exchange Learning Group: 304570962
You can add a cat qq:1764541256 or Znycat.
Let's study hard together.
Original: Http://blog.csdn.net/u013357243?viewmode=contents

(material source) Cat learn iOS (17) UI Pure code custom cell implementation Sina Weibo UI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.