iOS development--uitableviewcell Custom and compute frame

Source: Internet
Author: User
Tags uikit

I. Introduction of UITableViewCell

The contents of each line on the UITableView are UITableViewCell to show through the Uitableviewdatasource protocol method: Tableview:cellforrowatindexpath: To initialize the content to be displayed. UITableViewCell is shown by its own contentview, each cell has a contentview of sub-view by default, so the content displayed on each cell is added to this view.

There are four types of system UITableViewCell

Uitableviewcellstyledefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
Uitableviewcellstylesubtitle

These four types to try to see what is different, development is rarely used, because the system can not meet our needs, so we often customize UITableViewCell

Some of the uitableview belong to:

Sometimes we find a lot of uitableviewcell. The right side can display different icons, called accessors in iOS, and click to trigger different events, such as the settings of the iphone system

To set these icons you only need to set the Accesorytype property of UITableViewCell, Accesorytype is an enumeration type and has the following types:

 typedef Ns_ ENUM (Nsinteger, Uitableviewcellaccessorytype) {uitableviewcellaccessorynone,  //  No icons are displayed  uitableviewcellaccessorydisclosureindicator, //  Uitableviewcellaccessorydetaildisclosurebutton, //  content details icon and jump indicator icon  uitableviewcellaccessorycheckmark, //  Uitableviewcellaccessorydetailbutton Ns_enum_available_ios (7 _0) //  content detail icon };   

However, you will find that the Accesorytype without the first cell does not have this type, in fact, as long as you set UITableViewCell Accessoryview can let your cell display the control you want, as long as a uiview can support

Second, how to customize UITableViewCell

1. First you need to know what kind of cell you want to display

2. Define the controls you want to display as attributes

3. Since the development of the cell when assigning value is usually a corresponding model, so it is best to write a method specifically for the cell assignment

4. Because we may have a lot of controls when we customize the cell, we can write a class to calculate the frame of these controls, and then write a method to set the frame for those controls, and then call this method together when you call the cell assignment method

Here I simply imitate the QQ friend dynamic frame calculation

#import<Foundation/Foundation.h>@interfaceMzqqmodel:nsobject@property (nonatomic,copy) NSString*text; @property (nonatomic,copy) NSString*icon; @property (nonatomic,copy) NSString*Name: @property (nonatomic,copy) NSString*Picture ; @property (nonatomic,copy) NSString*Time ; @property (nonatomic,copy) NSString*expert;@end#import<UIKit/UIKit.h>@classMzqqmodel;@interfaceMzqqframe:nsobject@property (nonatomic, strong) Mzqqmodel*Qqmodel, @property (nonatomic, assign,ReadOnly) CGRect Iconframe;//the frame of the Avatar@property (nonatomic, assign,ReadOnly) CGRect Nameframe;//Nickname of Frame@property (nonatomic, assign,ReadOnly) CGRect timeframe;//Frame of Time@property (nonatomic, assign,ReadOnly) CGRect Expertframe;//QQ's talent frame@property (nonatomic, assign,ReadOnly) CGRect TextFrame;//frame of the body@property (nonatomic, assign,ReadOnly) CGRect Pictureframe;//frame of the picture@property (nonatomic, assign,ReadOnly) CGFloat RowHeight;//gotta be High@end#import "MZQQFrame.h"#import "MZQQModel.h"#defineMznamefont 15//the size of the nickname font#defineMztextfont 14//the size of the content font#defineKscreenwidth [UIScreen mainscreen].bounds.size.width@implementationMzqqframe//calculate the size of text-(Cgsize) Sizewithtext: (NSString *) Text maxSize: (cgsize) maxSize fontSize: (cgfloat) fontsize{//calculate the size of textCgsize namesize =[Text boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:@{    Nsfontattributename:[uifont Systemfontofsize:fontsize]} context:nil].size; returnnamesize;}- (void) Setqqmodel: (Mzqqmodel *) qqmodel{_qqmodel=Qqmodel; CGFloat margin=Ten; //AvatarCGFloat iconw = +; CGFloat Iconh= +; CGFloat IconX=margin; CGFloat Icony=margin; _iconframe=CGRectMake (IconX, Icony, Iconw, Iconh); //NicknameCgsize namesize =[Self sizeWithText:self.qqModel.name maxsize:cgsizemake (maxfloat, maxfloat) Fontsize:mznamefont]; CGFloat NameX= Cgrectgetmaxx (_iconframe) +margin; CGFloat Namey=Icony; _nameframe=CGRectMake (NameX, Namey, Namesize.width, namesize.height); //TimeCgsize timesize = [self sizeWithText:self.qqModel.time maxsize:cgsizemake (Kscreenwidth-cgrectgetmidx (_pictureframe) , maxfloat) FontSize: A]; CGFloat TimeX=NameX; CGFloat timey= Cgrectgetmaxy (_nameframe) +margin; _timeframe=CGRectMake (TimeX, Timey, Timesize.width, timesize.height); //TalentCGFloat EXPERTW = -; CGFloat Experth= -; CGFloat Experty=Namey; CGFloat Expertx= Cgrectgetmaxx (_nameframe) +margin; _expertframe=CGRectMake (Expertx, Experty, EXPERTW, Experth); //QQ ContentCgsize textSize = [self sizeWithText:self.qqModel.text maxsize:cgsizemake (Kscreenwidth-2*margin, maxfloat) Fontsize:mztextfont]; CGFloat TEXTX=IconX; CGFloat texty= Cgrectgetmaxy (_iconframe) +margin; _textframe=CGRectMake (TEXTX, texty, Textsize.width, textsize.height); //QQ pictures (Here the size of the picture given 100, the actual adjustment according to their own needs)    if(self.qqModel.picture) {cgfloat picturew= -; CGFloat Pictureh= -; CGFloat Picturex=IconX; CGFloat Picturey= Cgrectgetmaxy (_textframe) +margin; _pictureframe=(CGRect) {{Picturex,picturey},{picturew,pictureh}}; _rowheight= Cgrectgetmaxy (_pictureframe) +margin; }Else{_rowheight= Cgrectgetmaxy (_textframe) +margin; }    }@end#import<UIKit/UIKit.h>@classMzqqframe;@interfaceMzqqcell:uitableviewcell@property (nonatomic, strong) Mzqqframe*Qqframe;@end#import "MZQQCell.h"#import "MZQQFrame.h"#import "MZQQModel.h"@interfaceMzqqcell () @property (nonatomic, strong) Uiimageview*iconview;//Avatar@property (nonatomic, strong) UILabel *nameview;//Nickname@property (nonatomic, strong) UILabel *timeview;//Time@property (nonatomic, strong) Uiimageview *expertview;//Talent@property (nonatomic, strong) UILabel *textview;//Body@property (nonatomic, strong) Uiimageview *pictureview;//Image@end@implementationMzqqcell-(Uiimageview *) iconview{if(!_iconview) {_iconview=[[Uiimageview alloc] init]; }    return_iconview;}-(UILabel *) nameview{if(!_nameview) {_nameview=[[UILabel alloc] init]; }    return_nameview;}-(UILabel *) timeview{if(!_timeview) {_timeview=[[UILabel alloc] init]; }    return_timeview;}-(Uiimageview *) expertview{if(!_expertview) {_expertview=[[Uiimageview alloc] init]; }    return_expertview;}-(UILabel *) textview{if(!_textview) {_textview=[[UILabel alloc] init]; }    return_textview;}-(Uiimageview *) pictureview{if(!_pictureview) {_pictureview=[[Uiimageview alloc] init]; }    return_pictureview;}- (void) Setqqframe: (Mzqqframe *) qqframe{_qqframe=Qqframe; //set what the child controls display[self setsubviewscontent]; //set the child control's frame[self setsubviewsframe]; }- (void) setsubviewscontent{Mzqqmodel*qqmodel =Self.qqFrame.qqModel; Self.iconView.image=[UIImage ImageNamed:qqModel.icon]; Self.nameView.text=Qqmodel.name; Self.timeView.text=Qqmodel.time; //if Qqmodel.expert has a value for the picture    if(Qqmodel.expert) {Self.expertView.hidden=NO; Self.expertView.image=[UIImage ImageNamed:qqModel.expert]; }Else{Self.expertView.hidden=YES; }    if(_pictureview) {self.pictureView.image=[UIImage imageNamed:qqModel.picture]; }}- (void) setsubviewsframe{Self.iconView.frame=Self.qqFrame.iconFrame; Self.nameView.frame=Self.qqFrame.nameFrame; Self.timeView.frame=Self.qqFrame.timeFrame; Self.expertView.frame=Self.qqFrame.expertFrame; Self.textView.frame=Self.qqFrame.textFrame; Self.pictureView.frame=Self.qqFrame.pictureFrame; }

There are some ways to calculate a frame.

In fact, it is not very difficult to calculate frame, as long as you determine the position of the first control, you can based on the first control to the other control frame one by one calculated

Of course, the cell has a lot of properties and methods that are interesting to study on their own.

Custom and compute frame for iOS development--uitableviewcell

Related Article

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.