First, the UITableView property

Source: Internet
Author: User

First, the UITableView property

Nsindexpath type is used to get the user's choice of indexpath, in other functions, if you need to know which cell the user has chosen, it can be much easier to use it. You don't have to build the global variables section and row.

Nsindexpath *tableselection = [Self.tableview indexpathforselectedrow];

1. Initialization of the UITableView

UITableView tableview= [[UITableView alloc] Initwithframe:cgrectmake (0, 0, 320, 420)];

[TableView setdelegate:self];

[TableView setdatasource:self];

[Self.view Addsubview:tableview];

[TableView release];

(1) In the initialization of the UITableView must be implemented UITableView is, in the. h file to inherit Uitableviewdelegate and Uitableviewdatasource, and implement 3 UITableView data source method and set its delegate to self, this is the method that does not inherit uitableviewcontroller implementation directly.

(2) The Uitableviewcontroller is inherited directly when Xcode builds the project, it will help you to automatically write the method that UITableView must implement.

(3) UITableView inherits from Uiscrollview.

2. Data sources for UITableView

(1) UITableView is dependent on external resources to fill in the new table cell content, we call the data source, the data source can provide table cells according to the index path, in UITableView, the index path is Nsindexpath object, you can select segments or branches, That is the section and row in our code.

(2) UITableView has three core methods that must be implemented, respectively, as follows:

-(Nsinteger) Numberofsectionsintableview: (uitableview*) TableView;

This method can be displayed in segments or a single list to display our data.

-(Nsinteger) TableView: (uitableview*) Tableviewnumberofrowsinsection: (nsinteger) Section;

This method returns the number of rows per segment, and different segments return different rows, which can be done with a switch, and if a single list returns a single function you want.

-(uitableviewcell*) TableView: (uitableview*) Tableviewcellforrowatindexpath: (Nsindexpath *) Indexpath;

This method returns each cell that we call. Determined by the section and row of the path we indexed.

3. How to delegate UITableView

The delegate is used in response to user interaction actions, such as pulling down the data and selecting a row of cells, which is a great way to choose between UITableView.

(1) Explanation of the Entrustment method

Add index

-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{

return titledata;

}

Set the title for each section display

-(NSString *) TableView: (UITableView *) Tableviewtitleforheaderinsection: (Nsinteger) section{

return @ "andy-Breeze";

}

Specify how many partitions (section), default to 1

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {

return 2;

}

Specify how many rows are in each partition, default to 1

-(Nsinteger) TableView: (UITableView *) Tableviewnumberofrowsinsection: (Nsinteger) section{

}

Set the cell for each row call

-(UITableViewCell *) TableView: (UITableView *) Tableviewcellforrowatindexpath: (Nsindexpath *) Indexpath {

static NSString *simpletableidentifier = @ "Simpletableidentifier";

UITableViewCell *cell = [Tableviewdequeuereusablecellwithidentifier:

Simpletableidentifier];

if (cell = = nil) {

cell = [[[Uitableviewcellalloc] Initwithstyle:uitableviewcellstyledefault

Reuseidentifier:simpletableidentifier] autorelease];

}

cell.imageview.image=image;//picture When a cell is not selected

cell.imageview.highlightedimage=highlightimage;//the picture after the cell is selected

[Email protected] "andy-Breeze";

return cell;

}

Set Let UITableView line indent

-(Nsinteger) TableView: (UITableView *) Tableviewindentationlevelforrowatindexpath: (Nsindexpath *) indexpath{

Nsuinteger row = [Indexpath row];

return row;

}

Set the height of cell per line interval

-(CGFloat) TableView: (UITableView *) Tableviewheightforrowatindexpath: (Nsindexpath *) indexpath{

return 40;

}

Returns the currently selected cell

Nsindexpath *ip = [Nsindexpath indexpathforrow:row insection:section];

[Topicstable Selectrowatindexpath:ip Animated:YESscrollPosition:UITableViewScrollPositionNone];

Set the style of UITableView

[TableView Setseparatorstyle:uitableviewcellselectionstylenone];

Set the response event for the selected cell

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (nsindexpath*) indexpath{

[TableView deselectrowatindexpath:indexpath animated:yes];//color disappears immediately after selection

}

Set the action performed on the selected row

-(Nsindexpath *) TableView: (UITableView *) Tableviewwillselectrowatindexpath: (Nsindexpath *) Indexpath

{

Nsuinteger row = [Indexpath row];

return indexpath;

}

Sets whether the Del button appears in the scrub cell and can be processed in the deleted data

-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (nsindexpath*) Indexpath {

}

Edit state when setting delete

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle

Forrowatindexpath: (Nsindexpath *) Indexpath

{

Add an index table to the right

-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{

}

(2) Other

When the cell color is selected, the official document has the following options

typedef enum {

Uitableviewcellselectionstylenone,

Uitableviewcellselectionstyleblue,

Uitableviewcellselectionstylegray

} Uitableviewcellselectionstyle

Cell Right button format

typedef enum {

Uitableviewcellaccessorynone,//don ' t show any accessory view

Uitableviewcellaccessorydisclosureindicator,//regular chevron. Doesn ' t track

Uitableviewcellaccessorydetaildisclosurebutton,//blue button W/chevron. Tracks

Uitableviewcellaccessorycheckmark//checkmark. Doesn ' t track

} uitableviewcellaccessorytype

Whether to add line breaks

typedef enum {

Uitableviewcellseparatorstylenone,

Uitableviewcellseparatorstylesingleline

} Uitableviewcellseparatorstyle

Change Line Line Color

tableview.separatorcolor= [Uicolor Bluecolor];

Second, the agent method of UITableView

#pragma mark the height of each line

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

#pragma mark selects a row and then calls

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath

#pragma mark cancels a row, it calls

-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) Indexpath

#pragma mark is invoked when the user submits an edit (e.g. click on the "Delete" button)

As long as this method is implemented, the swipe delete function is added by default.

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath

#pragma mark, when you move a row, the cell calls

As long as this method is implemented, the sort function is added by default.

-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: ( Nsindexpath *) Destinationindexpath

Second, modify the state of the cell

1. It is best to modify the status of the cell by modifying the model data

2. Steps to Modify

1> Modifying model data

2> Refresh Table

* Overall Refresh: Reloaddata (most important)

* Local refresh: reloadrowsatindexpaths:withrowanimation:

Iii. Common methods of UITableView

1. Uncheck a row (remove the default blue background when cell is selected)

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

2. Partial refresh (refresh only rows loaded in the indexpaths array)

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

3. Overall refresh (each row in the screen is refreshed)

-(void) reloaddata;

4. Directly delete the number of lines on the interface (requires that the model data also delete the corresponding number)

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

5. Set edit mode

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

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

Attention:

Whether it's a partial refresh or a whole refresh, the principle is:

UITableView sends the corresponding message back to the data source (DataSource) and the agent (delegate), eventually presenting the resulting data

First, the UITableView property

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.