The table view (UITableView) in iOS uses a detailed

Source: Internet
Author: User

The table view (UITableView) in iOS uses a detailedRead Catalogue
    • First, the method of initialization
    • Second, Common properties
    • Three, the general method of explanation
    • Iv. Application of TableView operation Refresh Block
    • V. Editing operations for TableView
    • Vi. related operations of the selected cell
    • Vii. related methods of TableView attachment
UITableView in iOS use summary one, initialize method

-(Instancetype) initWithFrame: (CGRect) Frame style: (Uitableviewstyle) style;

This method initializes the frame size of the table view and sets a style, Uitableviewstyle is an enumeration, as follows:

?
1234 typedefNS_ENUM(NSInteger, UITableViewStyle) {    UITableViewStylePlain,                  // 标准的表视图风格    UITableViewStyleGrouped                 // 分组的表视图风格};
Back to top second, common properties

Gets the style of the table view (read-only property)

@property (nonatomic, readonly) Uitableviewstyle style;

Set up the representation diagram agent and the data source proxy (Agent method discussed later)

@property (nonatomic, assign) ID <UITableViewDataSource> dataSource;

@property (nonatomic, assign) ID <UITableViewDelegate> delegate;

Set the row height of the representation graph (default = 44)

@property (nonatomic) cgfloat rowHeight;

Sets the head view height and tail view height of the partition (only valid if the proxy method is not implemented)

@property (nonatomic) cgfloat sectionheaderheight;

@property (nonatomic) cgfloat sectionfooterheight;


Set an estimate of the row height (default = 0, which means no estimate, 7.0 is available)

@property (nonatomic) cgfloat estimatedrowheight;

Note: The official explanation of this property is that if your tableview line height is variable, then designing an estimated height can speed up the efficiency of your code.

The following two properties are similar to the above, respectively setting the estimated height of the partition header and tail views (available after 7.0)

@property (nonatomic) cgfloat estimatedsectionheaderheight; @property (nonatomic) cgfloat estimatedsectionfooterheight;

Set the position of a split line

@property (nonatomic) uiedgeinsets separatorinset;

If you are careful, you may find that the default TableView of the system has no top-to-edge on the left side of the split line. With this property, you can manually set the position offset of the split line, for example, you want to let the TableView split line display only the right half, you can set the following:

?
12 UITableView * tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];tab.separatorInset=UIEdgeInsetsMake(0, tab.frame.size.width/2, 0,0);

Set TableView background View view

@property (Nonatomic, ReadWrite, retain) UIView *backgroundview;

Back to top, back to the top. Three, the general method of explanation

Heavy Duty TableView

-(void) reloaddata;

Reload Index Bar

-(void) reloadsectionindextitles;

This method often adds new or deleted index categories without having to refresh the entire table view.

Get the number of partitions

-(Nsinteger) numberofsections;

Fetch rows by partition

-(Nsinteger) Numberofrowsinsection: (nsinteger) Section;

Gets the size of the partition (including header view, all rows, and tail view)

-(CGRect) Rectforsection: (nsinteger) Section;

view, tail view, and row height, respectively, based on partition

-(CGRect) Rectforheaderinsection: (nsinteger) Section;

-(CGRect) Rectforfooterinsection: (nsinteger) Section;

-(CGRect) Rectforrowatindexpath: (Nsindexpath *) Indexpath;

Get location information for a point in TableView

-(Nsindexpath *) Indexpathforrowatpoint: (cgpoint) point;

Get location information for a cell in TableView

-(Nsindexpath *) Indexpathforcell: (UITableViewCell *) cell;

Returns an array of information based on a rectangular range, where each row of rows is located

-(Nsarray *) Indexpathsforrowsinrect: (cgrect) rect;

Get cell by Location path

-(UITableViewCell *) Cellforrowatindexpath: (Nsindexpath *) Indexpath;

Get all the visible cells

-(Nsarray *) visiblecells;

Get location information for all visible rows

-(Nsarray *) indexpathsforvisiblerows;

view based on partition

-(Uitableviewheaderfooterview *) Headerviewforsection: (nsinteger) Section;

Get tail view based on partition

-(Uitableviewheaderfooterview *) Footerviewforsection: (nsinteger) Section;

To position the representation diagram to a location (row)

-(void) Scrolltorowatindexpath: (Nsindexpath *) Indexpath atscrollposition: (uitableviewscrollposition) Scrollposition animated: (BOOL) animated;

Note: The Indexpah parameter is the location of the anchor, determined by the partition and line number. The animated parameter determines whether there is an animation. The scrollposition parameter determines the relative position of the positioning, which makes an enumeration, as follows:

?
123456 typedefNS_ENUM(NSInteger, UITableViewScrollPosition) {    UITableViewScrollPositionNone,//同UITableViewScrollPositionTop    UITableViewScrollPositionTop,//定位完成后,将定位的行显示在tableView的顶部        UITableViewScrollPositionMiddle,//定位完成后,将定位的行显示在tableView的中间       UITableViewScrollPositionBottom//定位完成后,将定位的行显示在tableView最下面};

To position the representation diagram to the selected row

-(void) Scrolltonearestselectedrowatscrollposition: (uitableviewscrollposition) scrollposition animated: (BOOL) Animated;

This function is very similar to the above, except that it is positioning the representation graph to the selected row.

Back to top four, TableView operation refresh Block application

Before we introduce animation blocks, let's look at a few functions:

Insert Partition

-(void) Insertsections: (Nsindexset *) Sections withrowanimation: (uitableviewrowanimation) animation;

The animation parameter is an enumeration that enumerates the animation types as follows

?
12345678910 typedefNS_ENUM(NSInteger, UITableViewRowAnimation) {    UITableViewRowAnimationFade,//淡入淡出    UITableViewRowAnimationRight,//从右滑入    UITableViewRowAnimationLeft,//从左滑入    UITableViewRowAnimationTop,//从上滑入    UITableViewRowAnimationBottom,//从下滑入    UITableViewRowAnimationNone,  //没有动画    UITableViewRowAnimationMiddle,              UITableViewRowAnimationAutomatic = 100  // 自动选择合适的动画};

Delete Partition

-(void) Deletesections: (Nsindexset *) Sections withrowanimation: (uitableviewrowanimation) animation;

Overloading a partition

-(void) Reloadsections: (Nsindexset *) Sections withrowanimation: (uitableviewrowanimation) animation;

Move a partition

-(void) Movesection: (nsinteger) Section tosection: (Nsinteger) newsection;

Insert some rows

-(void) Insertrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;

Delete some rows

-(void) Deleterowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;

Overloading some rows

-(void) Reloadrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;

Move a row

-(void) Moverowatindexpath: (Nsindexpath *) Indexpath Toindexpath: (Nsindexpath *) Newindexpath;

Knowing the above functions, let's see what the action refresh block is:

When we call the above function, TableView will immediately invoke the proxy method to refresh, if we do the operation is to delete a row, but the data source array we may not refresh, the program will crash, because the agent returned the information and we delete does not match.

iOS provides us with the following two functions to solve this problem:

Start Block flag

-(void) beginupdates;

End Quick Sign

-(void) endupdates;

We can write all the actions we want to do in this block, so the agent refresh method is only invoked when the program executes to the end fast flag. The code examples are as follows:

?
1234 [tab beginUpdates];    [tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];    [dataArray removeObjectAtIndex:1];    [tab endUpdates];

Note: Do not call Reloaddata This method in this block, it will invalidate the animation.

Back to the top five, TableView edit operation

Set whether it is an edit state (a minus sign appears to the left of the cell in the edit state, click the Delete button on the right)

@property (nonatomic, getter=isediting) BOOL editing;

-(void) setediting: (BOOL) editing animated: (bool) animated;

Sets whether the cell can be selected (default = yes)

@property (nonatomic) BOOL allowsselection;

Set whether cell editing mode can be selected

@property (nonatomic) BOOL allowsselectionduringediting;

Set whether multiple selections are supported

@property (nonatomic) BOOL allowsmultipleselection;

Set whether multiple selections are supported in edit mode

@property (nonatomic) BOOL allowsmultipleselectionduringediting;

Back to top Vi. related actions for selected cells

Gets the location information for the selected cell

-(Nsindexpath *) Indexpathforselectedrow;

Get location information for multiple select cells

-(Nsarray *) indexpathsforselectedrows;

Code manually check and uncheck a row

-(void) Selectrowatindexpath: (Nsindexpath *) Indexpath animated: (BOOL) animated scrollposition: ( uitableviewscrollposition) scrollposition;

-(void) Deselectrowatindexpath: (Nsindexpath *) Indexpath animated: (BOOL) animated;

Note: These two methods will not callback the methods in the proxy.

Back to top Vii. related methods of TableView attachment

Set the minimum number of rows to display in the index bar

@property (nonatomic) Nsinteger Sectionindexminimumdisplayrowcount;

Set the index bar font color

@property (nonatomic, retain) Uicolor *sectionindexcolor;

Set the index bar background color

@property (nonatomic, retain) Uicolor *sectionindexbackgroundcolor;

Sets the color of the index bar when it is selected

@property (nonatomic, retain) Uicolor *sectionindextrackingbackgroundcolor;

Set the style of a split line

@property (nonatomic) Uitableviewcellseparatorstyle Separatorstyle;

This style is an enumeration, as follows:

?
12345 typedefNS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {    UITableViewCellSeparatorStyleNone,//无线    UITableViewCellSeparatorStyleSingleLine,//有线    UITableViewCellSeparatorStyleSingleLineEtched  };

Set the split Line color

@property (nonatomic, retain) Uicolor *separatorcolor;

Set split line frosted glass effect (available after IOS8)

@property (nonatomic, copy) Uivisualeffect *separatoreffect;

Note: This property is IOS8 after the new.

Set TableView head View

@property (nonatomic, retain) UIView *tableheaderview;

Set TableView tail View

@property (nonatomic, retain) UIView *tablefooterview;

Take a cell from the reuse pool

-(ID) Dequeuereusablecellwithidentifier: (NSString *) identifier;

Gets a registered cell

-(ID) Dequeuereusablecellwithidentifier: (NSString *) identifier Forindexpath: (nsindexpath*) Indexpath

or tail views from a reuse pool

-(ID) Dequeuereusableheaderfooterviewwithidentifier: (NSString *) identifier;

Registering a cell with the Xib file

-(void) registernib: (uinib *) nib Forcellreuseidentifier: (NSString *) identifier;

Registering a cell with the OC class

-(void) RegisterClass: (Class) Cellclass forcellreuseidentifier: (NSString *) identifier

The above two methods are the methods after IOS6.

Get registered head view and tail view through Xib file and OC class

-(void) registernib: (uinib *) nib Forheaderfooterviewreuseidentifier: (NSString *) identifier;

-(void) RegisterClass: (Class) AClass forheaderfooterviewreuseidentifier: (NSString *)

About TableView's proxy method, because of the length of reasons, summarized in the next blog.

From: http://my.oschina.net/u/2340880/blog/404605

The table view (UITableView) in iOS uses a detailed

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.