One of the most common controls for displaying data lists in iOS, which supports vertical scrolling
UITableViewTwo of the built-in styles
Uitableviewstyleplain uitableviewstylegrouped
Data source
(DataSource)and agents
(delegate)
L? UITableView requires a data source (DataSource) to display the data, UITableView queries the data source for the total number of rows of data and what data is displayed for each row. A uitableview that does not have a data source set is just an empty shell. Any OC object that complies with the Uitableviewdatasource protocol can be a UITableView data source
L? You typically set up a proxy object (delegate) for UITableView so that you can handle it when the UITableView triggers an event, such as selecting a row. Any OC object that complies with the Uitableviewdelegate protocol can be a proxy object for UITableView
L? Typically allows the controller to act as UITableView DataSource and delegate
Uitableviewdatasource
L?
@required
U? -(Nsinteger)
TableView:(uitableview*) TableView
numberofrowsinsection:(Nsinteger) Section;
How many rows are there in section partition
U? -(uitableviewcell*)
TableView:(uitableview*) TableView
Cellforrowatindexpath:(Nsindexpath *) Indexpath;
Create the UITableViewCell object (Indexpath contains section and row) on row row of section partition
L?
@optional
U? -(Nsinteger)
Numberofsectionsintableview:(UITableView
*) TableView; Total number of partitions
U? -(nsstring*)
TableView:(uitableview*) TableView
titleforheaderinsection:(Nsinteger) Section; Header headings for section sections
Uitableviewdatasource
U? -(nsstring*)
TableView:(uitableview*) TableView
titleforfooterinsection:(Nsinteger) Section;
The bottom title of section sections
U? -(BOOL)
TableView:(uitableview*) TableView
Caneditrowatindexpath:(Nsindexpath *) Indexpath;
Whether a row can be edited (deleted)
U? -(BOOL)
TableView:(uitableview*) TableView
Canmoverowatindexpath:(Nsindexpath *) Indexpath;
Whether a row can be moved to reorder
U? -(nsarray*)
Sectionindextitlesfortableview:(UITableView *) TableView;
UITableView the contents of the index bar to the right
uitableviewdelegate
L? -(void)
TableView:(UITableView *) TableView
Didselectrowatindexpath:(Nsindexpath *) Indexpath
A row of UITableView has been selected
L? -(CGFloat)
TableView:(UITableView *) TableView
Heightforrowatindexpath:(Nsindexpath *) Indexpath the height of a row
L? -(CGFloat)
TableView:(UITableView *) TableView
heightforheaderinsection:(Nsinteger) Section
Height of section header
L? -(CGFloat)
TableView:(UITableView *) TableView
heightforfooterinsection:(Nsinteger) The height of the tail of section
uitableviewdelegate
L? -(UIView *)
TableView:(UITableView *) TableView
viewforheaderinsection:(Nsinteger) Section
View of section header display
L? -(UIView *)
TableView:(UITableView *) TableView
viewforfooterinsection:(Nsinteger) Section
The view that appears at the end of section partition
L? -(Nsinteger)
TableView:(UITableView *) TableView
Indentationlevelforrowatindexpath:(Nsindexpath *) Indexpath
Sets the level indent for each row (the smaller the number, the higher the rank)
UITableViewCell
L? Each line of UITableView is a UITableViewCell, through DataSource's
TableView:
Cellforrowatindexpath: Method to initialize each row
L? UITableViewCell is a subclass of UIView, with a default child view inside:
Contentview。 Contentview is the parent view of the content displayed by UITableViewCell and is responsible for displaying some of the secondary indication views. The function of the secondary indicator view is to display an icon that represents an action, which can be set by setting the UITableViewCell
AccessorytypeTo show that the default is Uitableviewcellaccessorynone (does not show the secondary indicator view), the other values are as follows:
U? Uitableviewcellaccessorydisclosureindicator
U? Uitableviewcellaccessorydetaildisclosurebutton u? Uitableviewcellaccessorycheckmark
UITableViewCellOf
Contentview
L? Contentview has 3 sub-views by default, 2 of which are Uilabel (accessed through UITableViewCell's Textlabel and Detailtextlabel properties), and the 3rd is Uiimageview ( Accessed via UITableViewCell's ImageView property)
L? UITableViewCell also has a Uitableviewcellstyle property that determines which child views of Contentview are used and where they are located in Contentview
Uitableviewcellstyledefault
Uitableviewcellstylesubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
UITableViewCellHow objects are reused
L? iOS devices have limited memory, and if you use UITableView to display thousands of data, you need thousands of UITableViewCell objects, which will deplete the memory of your iOS device. To work around this problem, you need to reuse the UITableViewCell object
L? Reuse principle: When scrolling the list, some UITableViewCell will move out of the window, and 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,datasource in the pool that will be configured with the new data UITableViewCell, and then returns to the UITableView, re-displayed to the window, avoiding the creation of a new object
UITableViewCellHow objects are reused
L? There is also a very important question: Sometimes you need to customize UITableViewCell (inherit UITableViewCell with a subclass), and each line is not necessarily the same uitableviewcell (such as SMS chat layout), So a uitableview may have different types of uitableviewcell, and there will be many different types of UITableViewCell in the object pool, so uitableview can reuse UITableViewCell Will get the wrong type of UITableViewCell
L? Solution: UITableViewCell has a nsstring *reuseidentifier property that allows you to set reuseidentifier by passing in a specific string identifier when initializing UITableViewCell ( Generally used UITableViewCell class name). When UITableView asks DataSource to return UITableViewCell, the UITableViewCell object of the corresponding type is first identified by a string to the object pool, if there is one, reuse, if not, pass Into this string identifier to initialize a UITableViewCell object
Reuse
UITableViewCellObject
-(UITableViewCell *)
TableView:(UITableView *) TableView
Cellforrowatindexpath:(Nsindexpath *) Indexpath {
static NSString *identifier = @ "UITableViewCell";
UITableViewCell *cell = [TableView
Dequeuereusablecellwithidentifier: identifier];
if (cell = = nil) {
cell = [[[UITableViewCell Alloc]
Initwithstyle: Uitableviewcellstyledefault
Reuseidentifier: identifier] autorelease];
}
Cell.textLabel.text = [NSString stringwithformat:@ "Text%i",
Indexpath.row];
return cell;
}
UITableViewCellThe Common Properties
L? Set background
U?
Backgroundview
L? Set the background view when selected
U?
Selectedbackgroundview
L?
Selectionstyleproperty to set the background color when UITableViewCell is selected: U? Uitableviewcellselectionstylenone No Color
U? Uitableviewcellselectionstyleblue Blue (default)
U? Uitableviewcellselectionstylegray Grey
Custom
UITableViewCellL? There are generally two ways:
1 Use a xib file to express the contents of the UITableViewCell in this set string identifier for reuse
2 Add a child view to the UITableViewCell Contentview by code, in the initialization method (e.g.
Init、
Initwithstyle:reuseidentifier:) to add a child control in the
layoutsubviewsThe location and size of the child controls are allocated in the
UITableViewThe editing mode
L? UITableView has a
editingproperty, set to Yes, to enter edit mode. In edit mode, you can manage rows in a table, such as changing the order of rows, adding rows, deleting rows, but not modifying the contents of a row
L? Multiple ways to turn on edit mode
U? @property (nonatomic,getter=isediting) boolediting
U? -(void) setediting: (BOOL) editinganimated: (BOOL) animated
Delete
UITableViewThe line
L? The first thing to do is open edit mode
L? Here's how to implement Uitableviewdatasource:
-(void)
TableView:(UITableView *) TableView
Commiteditingstyle:(Uitableviewcelleditingstyle) Editingstyle
Forrowatindexpath:(Nsindexpath *) Indexpath {
If the UITableView commits a delete instruction
if (Editingstyle = = Uitableviewcelleditingstyledelete) {
Delete real Data
[Self.data RemoveObjectAtIndex:indexPath.row];
Delete a row in the UITableView (draw effect)
[TableView Deleterowsatindexpaths:[nsarray
Arraywithobject:indexpath]
Withrowanimation:uitableviewrowanimationleft];
If the animation effect is not considered, it can also be directly [TableView reload]; }
}
Move
UITableViewThe line
L? The first thing to do is open edit mode
L? The following method of implementing Uitableviewdatasource (if this method is not implemented, the line will not be wrapped)
-(void)
TableView:(UITableView *) TableView
Moverowatindexpath:(Nsindexpath *) Sourceindexpath
Toindexpath:(Nsindexpath
*) Destinationindexpath {
int from = Sourceindexpath.row; int to = Destinationindexpath.row; if (from = = to) return;
Exchanging data
[Self.data Exchangeobjectatindex:from
Withobjectatindex:to];
}
Selected
UITableViewThe line
L? This method is called when a row is selected (Uitableviewdelegate method)
-(void)
TableView:(UITableView *) TableView
Didselectrowatindexpath:(Nsindexpath *) Indexpath {
Uncheck a row to make the highlighted color of the selected row disappear (Draw effect) [TableView Deselectrowatindexpath:indexpath
Animated:yes];
}
UITableViewCommon methods
L? -(ID)
initWithFrame:(CGRect) frame
style:(Uitableviewstyle) style Initializes a UITableView and sets the table style
L? -(void)
ReloaddataRe-access the data source, refresh the interface
L? -(Nsinteger)
numberofsectionsNumber of partitions
L? -(Nsinteger)
numberofrowsinsection:(Nsinteger) Section
Number of rows in section partition
L? -(UITableViewCell *)
Cellforrowatindexpath:(Nsindexpath *) Indexpath
Find the corresponding UITableViewCell object through Indexpath
L? -(void)
setediting:(BOOL) Editing
animated:(BOOL) animated
Do you want to turn on edit mode
L? -(void)
Deselectrowatindexpath:(Nsindexpath *) Indexpath
animated:(BOOL) animated
Uncheck a row to make the highlighted color of selected rows disappear (Draw effect)
UITableViewCommon methods
L? -(ID)
Dequeuereusablecellwithidentifier:(NSString *) identifier
Find the corresponding UITableViewCell object in the (cache) pool by identifier
L? -(void)
deleterowsatindexpaths:(Nsarray *) indexpaths
withrowanimation:(uitableviewrowanimation) Animation
Remove all rows in the Indexpaths range
L? @property (nonatomic,readonly)
Uitableviewstyle StyleTable Style
L? @property (nonatomic,assign)
ID <UITableViewDataSource> dataSourceData source
L? @property (nonatomic,assign)
ID <UITableViewDelegate> DelegateAgent
L? @property (nonatomic,getter=
isediting)
BOOL Editingis edit mode
L? @property (nonatomic)
Uitableviewcellseparatorstyle SeparatorstyleSet the style of the divider line
L? @property (Nonatomic,retain)
Uicolor *separatorcolorSet the color of the divider line
UITableViewCommon methods
L? @property (Nonatomic,retain)
UIView *tableheaderviewThe view that the table header displays
L? @property (Nonatomic,retain)
UIView *tablefooterviewView of footer Display
L? @property (nonatomic)
BOOL allowsselection
Whether to allow row selection
L? @property (nonatomic)
BOOL allowsselectionduringeditingWhether to allow row selection in edit mode
L? @property (nonatomic)
BOOL allowsmultipleselectionWhether to allow multiple rows to be selected
L? @property (nonatomic)
BOOL allowsmultipleselectionduringeditingAllow multiple lines to be selected in edit mode
Uitableviewcontroller
L? is a subclass of Uiviewcontroller, Uitableviewcontroller plays 3 roles by default: View controller, UITableView data source, and proxy
L? Uitableviewcontroller's view is a uitablview, and Uitableviewcontroller is responsible for setting and displaying the object. When the Uitableviewcontroller object is created, the DataSource and delegate of the UITableView object are directed to uitableviewcontroller themselves