First, the creation of UITableView
- A table control must specify a style when it is created, using only the following instantiation methods
[[UITableView alloc]initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];
Two styles of UITableView
1> UITableViewStylePlain 2> UITableViewStyleGrouped
Common Properties for 2.UITableView
//Head view (AD)@property(nonatomic, retain)UIView*tableheaderview;//Show the status of the load@property(nonatomic, retain)UIView*tablefooterview;//height of each row@property(nonatomic)CGFloatRowHeight;//Group head height (QQ friends list)@property(nonatomic)CGFloatSectionheaderheight;//Group tail height@property(nonatomic)CGFloatSectionfooterheight;
3. Common methods
// 用于从缓存池中取出可用的cell- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
Second, UITableView display data
- UITableView presentation data requires a data source datasource
- Compliance with Data source protocol Uitableviewdatasource and methods of implementing data sources
1. Compliance with Data source protocol
<UITableViewDataSource>
selfself;
2. Implementing a data source approach
//How many sets of data are there altogether- (Nsinteger) Numberofsectionsintableview: (UITableView*) TableView;//How many rows of data are in each group- (Nsinteger) TableView: (UITableView*) TableView numberofrowsinsection: (Nsinteger) Section;//What is displayed in each row- (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) Indexpath;//height of each row- (CGFloat) TableView: (UITableView*) TableView Heightforrowatindexpath: (Nsindexpath*) Indexpath//Select data for a row- (void) TableView: (UITableView*) TableView Diddeselectrowatindexpath: (Nsindexpath*) Indexpath//group header title- (NSString*) TableView: (UITableView*) TableView titleforheaderinsection: (Nsinteger) Section//Group Trailer header- (NSString*) TableView: (UITableView*) TableView titleforfooterinsection: (Nsinteger) Section//Rvalue index list- (Nsarray*) Sectionindextitlesfortableview: (UITableView*) TableView;//Group head view- (UIView*) TableView: (UITableView*) TableView viewforheaderinsection: (Nsinteger) Section;//Group trailer View- (UIView*) TableView: (UITableView*) TableView viewforfooterinsection: (Nsinteger) Section;//Group head height- (CGFloat) TableView: (UITableView*) TableView heightforheaderinsection: (Nsinteger) Section;//Group tail height- (CGFloat) TableView: (UITableView*) TableView heightforfooterinsection: (Nsinteger) Section;
Third, the creation of UITableViewCell
Common Properties for 1.cell
//Icon@property(nonatomic,ReadOnly, retain)Uiimageview*imageview;//Title@property(nonatomic,ReadOnly, retain)UILabel*textlabel;//Detail title@property(nonatomic,ReadOnly, retain)UILabel*detailtextlabel;//Content view, which contains the 3 views above@property(nonatomic,ReadOnly, retain)UIView*contentview;//The entrance view on the right@property(nonatomic, retain)UIView*accessoryview;
2. How to create a cell
1>
// cell是通过手写代码创建的,才会调用这个方法来初始化cell[[UITableViewCell alloc] initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(NSString *)#>];
The Uitableviewcellstyle property determines which child views of the Contentview are used, and the location of those child views
// 标题+图像// 标题+明细+图像UITableViewCellStyleValue1 // 标题+明细+图像UITableviewCellStyleValue2 // 标题+明细reuseIdentifier:用于cell循环利用的标示ID
2>
// cell是通过storyboard或则xib实现的,就会调用这个方法来初始化cell- (void)awakeFromNib{}
3>
// 在这方法中设置子控件的frame- (void)layoutSubViews{}
The principle of 3.Cell reuse
1> when scrolling the list, some UITableViewCell will move out of the window, UITableView will place the UITableViewCell outside the window into an object pool for reuse. When UITableView requires DataSource to return UITableViewCell, DataSource will look at the object pool first, if there are unused uitableviewcell in the pool, DataSource will configure the UITableViewCell with the new data and return it to the UITableView and back to the window to avoid creating new objects
2> also has a very important problem: sometimes you need to customize UITableViewCell (inherit UITableViewCell with a subclass), and each row is not necessarily the same uitableviewcell, So a uitableview may have different types of uitableviewcell, and there will be many different types of UITableViewCell in the object pool, Then UITableView may get the wrong type when reusing UITableViewCell UITableViewCell
Solution:
UITableViewCell has a NSString *reuseidentifier property that allows you to set reuseidentifier by passing in a specific string identifier when the UITableViewCell is initialized ( Generally used UITableViewCell class name). When UITableView requires DataSource to return UITableViewCell, a string is first identified to the object pool to find the corresponding type of UITableViewCell object, if there is, reuse, if not, The string identifier is passed in to initialize a UITableViewCell object
Reuse code for 4.Cell
-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (nsindexpath *) indexpath{//1. Define the identity of a cell static nsstring *id = @ "Mjcell" ; //2. Remove the cell from the cache pool uitableviewcell *cell = [TableView dequeuereusablecellwithidentifier:id]; //3. If there is no cell in the cache pool if (cell = = nil ) {cell = [[uitableviewcell alloc] Initwithstyle:uitableviewcellstylesubtitle Reu Seidentifier:id]; } //4. Set the properties of the cell ... return Cell;}
Iv. Custom implementation of cell
1.xib implementation of UITableViewCell (cell's high consistency)
1> Create a new xib file describes the internal structure of a view (suppose called wbtgcell . xib ) 2> New custom class, class name and xib file name consistency wbtgcell 3 > will xib and the custom. m file to connect and specify reusable identifiers 4> Provides a class method to return a well-created custom view (masking xib loading process) 5> provides a model property to accept data from the outside world 6> Override the model property of the setter method, in setter method to present the model data to the corresponding child control
2. Customizing cells by code (cell height inconsistency)
1>Create a new inherit fromUITableViewCellThe class2>RewriteInitwithstyle:reuseidentifier: Method * Adds all the child controls that need to be displayed (you do not need to set the data for the child controls andFrame, the child controls are added to theContentview* For child controls one-time property settings (some properties only need to be set once, such as fonts, fixed images)3>Provide2Model * Data Model: Storing text data \ picture Data *FrameModel: holds the data model \ All child controlsFrame\CellThe height4>CellHave aFrameModel (do not have a data model directly)5>RewriteFrameof the model propertiesSetterMethod: In this method, set the display data of the child control andFrame6>FrameThe initialization of model data has been done by lazy loading methods (eachCellcorresponding to theFrameModel data is loaded only1Times
V. Static cells
1> sets the content property of the TableView by not storyboard, static cells, dynamic is dynamically added
2> static cell applies to tableview inside the cell content and the reality is fixed, subsequent does not need to change the contents inside the cell
3> You cannot use a static cell if the content displayed in the TableView needs to be changed
Six, dynamic cell
Application Management
1>cell重用容易出现从缓存池中取出来的cell 可能是之前更改过状态的cell,所以在重用的时候必须覆盖所有cell中子控件的状态。2>当cell从storyboard中tableviewcontroller中唯一标示中取出来的时候,控制器可以不做初始化操作,ifnil){};因为storyboard中确实已经存在了cell了3>storyBoard中自定义cell的时候,可以创建多个cell的模型,然后根据唯一标示可以取出相应的cell4>按钮能不能用取决于当前cell有没有下载
Add:
1, the size of the string
// NSString字符串中对象方法:- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize{ NSDictionary *attrs = @{NSFontAttributeName : font}; return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;}
2, the image of the random non-deformation (direct Xcode set the picture on the line)
+ (UIImage *)resizableImage:(NSString *)name{ UIImage *normal = [UIImage imageNamed:name]; CGFloat w = normal.size.width0.5; CGFloat h = normal.size.height0.5; return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
4. IOS Development UITableView