Summary of various methods commonly used in Ios-uitableview development

Source: Internet
Author: User

There are two ways of implementing a list

Inherit Uiviewcontroller, implement Uitableviewdatasource and Uitableviewdelegate protocol. DECLARE UITableView.

UserInfoViewController.h

@interface UserInfoViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{}@end

Userinfoviewcontroller.m

@interface UserViewController (){}@property(nonatomic,strong)UITableView *tableView;@end@implementation UserViewController- (void)viewDidLoad {    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.heigh style:UITableViewStyleGrouped];    self.tableView.delegate=self;    self.tableView.dataSource=self;    [self.view addSubview:self.tableView];}@end

Way Two

Inheritance Uitableviewcontroller,uitableviewcontroller The default implementation of the Uitableviewdatasource and Uitableviewdelegate protocols. The default declaration is UITableView.

UserViewController.h

@interface UserInfoViewController : UITableViewController@end

Userviewcontroller.m

Uitableviewdatasource

It mainly provides display data (UITableViewCell) for UITableView, specifying the type of editing operations supported by UITableViewCell (Insert,delete and reordering), And according to the user's actions for the corresponding data update operation, if the data is not more action to correct the update, may lead to display exceptions, or even crush.

The method that must be implemented//returns the list to display the number of rows-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section;-// Returns the uitableviewcell-(UITableViewCell *) displayed per row TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath; Select the Implementation method//Returns the number of sections in the list, returning by default 1-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview;// Set Title header name-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section;//set the name of the header pin-( NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) section//whether the row of the list is supported for increment, delete function-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath;//whether to support the move order function for rows in the list-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath;//adjust data based on edit style-(void) TableView: ( UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: ( Nsindexpath *) indexpath;//adjusts the data according to the Nsindexpath before and after the move-(void) TableView: (UITableView *) TableView Moverowatindexpath: (NSINdexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) Destinationindexpath; 
Uitableviewdelegate

It mainly provides some optional methods to control the selection of the TableView, to specify the display of the header and tail of the section, and to assist in the removal and sequencing of the cell.

/*-----Custom display, you can display different colors in interlaced----*/-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) indexpath;--(void) TableView: (UITableView *) TableView Willdisplayheaderview: ( UIView *) View forsection: (nsinteger) Section ns_available_ios (6_0);--(void) TableView: (UITableView *) TableView Willdisplayfooterview: (UIView *) View forsection: (nsinteger) Section ns_available_ios (6_0);--(void) TableView: ( UITableView *) TableView Didenddisplayingcell: (UITableViewCell *) cell Forrowatindexpath: (nsindexpath*) IndexPath NS_ Available_ios (6_0);--(void) TableView: (UITableView *) TableView Didenddisplayingheaderview: (UIView *) View forsection :(Nsinteger) Section ns_available_ios (6_0);--(void) TableView: (UITableView *) TableView Didenddisplayingfooterview: ( UIView *) View forsection: (nsinteger) Section ns_available_ios (6_0);/*-----Returns the height of each row, table header, footer----*/-(CGFloat) TableView: ( UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath;-(cgfloat) TABleview: (UITableView *) TableView heightforheaderinsection: (Nsinteger) section;-(cgfloat) TableView: (UITableView *) TableView heightforfooterinsection: (nsinteger) section;/*-----Returns the estimated height of each row, table header, footer----*/-(CGFloat) TableView: ( UITableView *) TableView Estimatedheightforrowatindexpath: (Nsindexpath *) Indexpath Ns_available_ios (7_0);-(CGFloat) TableView: (UITableView *) TableView estimatedheightforheaderinsection: (nsinteger) Section ns_available_ios (7_0);-( CGFloat) TableView: (UITableView *) TableView estimatedheightforfooterinsection: (nsinteger) Section Ns_available_ios ( 7_0);/*-----Custom View for header----*/-(Nullable UIView *) TableView: (UITableView *) TableView viewforheaderinsection   :(Nsinteger) Section; -(Nullable UIView *) TableView: (UITableView *) TableView viewforfooterinsection: (nsinteger) Section; /*-----checked and unchecked----*/-(Nullable Nsindexpath *) TableView: (UITableView *) TableView Willselectrowatindexpath: ( Nsindexpath *) indexpath;-(Nullable Nsindexpath *) TableView: (UITableView *) TableView WilldeSelectrowatindexpath: (Nsindexpath *) Indexpath Ns_available_ios (3_0);-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath;-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) Indexpath Ns_available_ios (3_0);/*-----Set the style when editing state, Can return Uitableviewcelleditingstyleinsert (indicates an increase) Uitableviewcelleditingstyledelete (for deletion) Uitableviewcelleditingstyledelete | Uitableviewcelleditingstyleinsert (indicates multiple selection)----*/-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath;/*-----Set the name of the delete button----*/-(nullable NSString *) TableView: ( UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) Indexpath NS_AVAILABLE_ IOS (3_0);/*-----Set multiple buttons on sliding delete----*/-(Nullable Nsarray *) TableView: (UITableView *) TableView Editactionsforrowatindexpath: (Nsindexpath *) Indexpath Ns_available_ios (8_0);
UITableView do not display redundant table split lines
UIView *view =[ [UIView alloc]init];view.backgroundColor = [UIColor clearColor];[tableView setTableFooterView:view];[tableView setTableHeaderView:view];
Set UITableView Split line interval

When we use TableView, we find that the left side of the split line is shorter and can usually be solved using Setseparatorinset:uiedgeinsetszero. But after upgrading to XCode6, the iOS8 found no effect. Here are the solutions:

First, add the following code to the Viewdidlayoutsubviews method:

- (void) viewDidLayoutSubviews {    [super viewDidLayoutSubviews];    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,10,0,10)];    }    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,10,0,10)];    }}

Then add the following code to the Willdisplaycell method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {        [cell setSeparatorInset:UIEdgeInsetsMake(0,10,0,10)];    }    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {        [cell setLayoutMargins:UIEdgeInsetsMake(0,10,0,10)];    }}
UITableViewCell re-use mechanism

When we write UITableViewCell, we usually declare uitableviewcell like the code below, so what can be written to solve the problem?

static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)identifier#>]if (cell == nil) {    <#statements#>}

The answer to this question is at the heart of what kind of problem this mechanism solves. Key points in the "number of cells displayed on one screen" is limited when the screen scrolls, the method is called to get the new cell, and the old cell is not displayed outside the screen.

This is the reuse mechanism. When the cell needs to be displayed, from the queue to find, found, set the content, display the scrolling interface when a cell is removed from the screen, the cell is dropped into the queue to show the new cell, if there is "the same type" (identifier) cell, Just take one out of the queue, set up the data, show how many cells there will be in a queue, this will automatically control

Note that the queue contains an instance of the cell, not a "prototype", so it says "assume 5 per page." The 6th is the 1th cell, and 7th is the 2nd; "The result is that no matter how many rows you have in your table, the memory actually needs to store just one screen so many rows of cells are done."

Gets the coordinates of UITableViewCell relative to UITableView

The coordinates obtained by using the UITableViewCell frame property are invariant. That is, if the UITableView initialization is complete, the coordinates of each cell are fixed, x is unchanged, and y increments with index.

After testing, any cell dragged or slid to any relative position of UITableView, the cell's Frame property has not changed.

How do I get the coordinates of UITableViewCell relative to UITableView?

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];   CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
UITableView get Indexpath by long-press gesture
- (void)longPress:(UILongPressGestureRecognizer *)recognizer{if (recognizer.state == UIGestureRecognizerStateBegan) {//通过定位获取indexPathCGPoint point = [recognizer locationInView:self.tableView]; self.cellIndexPath=[self.tableView indexPathForRowAtPoint: point];}}
Table Refresh
//整表刷新[self.tableView reloadData];//当行刷新[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
Table Delete
[self.tableView beginUpdates];[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[self.deleteDic allKeys]] withRowAnimation:UITableViewRowAnimationFade];[self.tableView endUpdates];

These two methods, which are used together, are tagged with an animated block of TableView. Represents the start and end of an animation, respectively. Both appear in pairs, and can be used in a nested set.

Generally, in addition, delete, select TableView to use, and achieve animation effects. Within an animation block, the Reloaddata method is not recommended and, if used, affects the animation.

Summary of various methods commonly used in Ios-uitableview development

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.