This article presents a summary of the basic usage methods of UITableView in iOS development. First, the controller needs to implement two delegate, namely Uitableviewdelegate and Uitableviewdatasource, and then the delegate of the UITableView object to be set to self And then you can implement some of these delegate to pull.
UITableView Basic Use Method
1. First, the controller needs to implement two delegate, namely Uitableviewdelegate and Uitableviewdatasource
2. Then the delegate of the UITableView object to be set to self.
3. Then you can implement some of these delegate to pull.
(1)-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView;
This method returns the number of sections that TableView has
- Returns the number of sections
- -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
- {
- return 1;
- }
(2)-(Nsinteger) TableView: (UITableView *) Table numberofrowsinsection: (nsinteger) Section;
This method returns the number of elements in the corresponding section, which is the number of rows.
- -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
- {
- return 10;
- }
(3)-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (nsindexpath*) Indexpath;
This method returns the height of the specified row.
-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section;
This method returns the height of the header view for the specified section.
-(CGFloat) TableView: (UITableView *) TableView heightforfooterinsection: (nsinteger) Section;
This method returns the height of the footer view for the specified section.
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- Static NSString * Showuserinfocellidentifier = @"Showuserinfocell";
- UITableViewCell * cell = [Tableview_ dequeuereusablecellwithidentifier:showuserinfocellidentifier];
- if (cell = = nil)
- {
- Create a cell to the display an ingredient.
- cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle
- Reuseidentifier:showuserinfocellidentifier]
- Autorelease];
- }
- Configure the cell.
- [Email protected] "signature";
- Cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str () Encoding: Nsutf8stringencoding];
- }
(5)-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section
Returns the height of the header of the specified section
- -(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section
- {
- if (section ==0)
- return 80.0f;
- Else
- return 30.0f;
- }
(6)-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
Returns the title of the header of the specified section, and if the section header has a return view, the title will not work.
- -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
- {
- if (TableView = = tableview_)
- {
- if (section = = 0)
- {
- return @"Title 1";
- }
- else if (section = = 1)
- {
- return @"Title 2";
- }
- Else
- {
- return nil;
- }
- }
- Else
- {
- return nil;
- }
- }
(7)-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
Returns the view of the specified section header, and if not, the function can not return the view
- -(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
- {
- if (section = = 0)
- {
- uiview* Header = [[[NSBundle Mainbundle] loadnibnamed: @"Settingheaderview"
- Owner:self
- Options:nil] Lastobject];
- Else
- {
- return nil;
- }
- }
(8)-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
When the user selects a cell in a row, the callback uses this. But first, you must set a property of TableView to select only.
- Tableview.allowsselection=yes;
- Cell.selectionstyle=uitableviewcellselectionstyleblue;
If you do not want to respond to select, you can set the property with the following code:
- Tableview.allowsselection=no;
Here is the response to the Select Click Function, according to which section, which row will respond to itself.
- -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
- {
- if (indexpath.section = = 1)
- {
- Return
- }
- else if (indexpath.section==0)
- {
- Switch (Indexpath.row)
- {
- Chat
- Case 0:
- {
- [Self ontalktofriendbtn];
- }
- Break
- Default
- Break
- }
- }
- Else
- {
- return;
- }
- }
How to allow the cell to respond to select, but the selected color does not change, then set
Cell.selectionstyle = Uitableviewcellselectionstylenone;
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- The color of the cell is not changed after it is selected
- Cell.selectionstyle = Uitableviewcellselectionstylenone;
- }
(9) How to set the split line between each line of TableView
- Self.tableview.separatorstyle=uitableviewcellseparatorstylesingleline;
If you do not need to split the line, then set the property to Uitableviewcellseparatorstylenone.
(10) How to set the background color of TableView cell
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- Set Background color
- Cell.contentview.backgroundcolor=[uicolor colorwithred:0.957 green:0.957 blue:0.957 alpha:1];
- }
(one)-(void) TableView: (UITableView *) TableView Accessorybuttontappedforrowwithindexpath: (Nsindexpath *) Indexpath
This function responds, the user clicks the arrow to the right of the cell (if any)
(12) How to set TableView can be edited
The first step is to enter edit mode:
- [TableView Setediting:yes Animated:yes];
If you want to exit edit mode, it must be set to No
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath
Returns the type of edit the current cell is performing, and the following code returns the Delete mode
- -(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath
- {
- return uitableviewcelleditingstyledelete;
- }
-(void) TableView: (UITableView *) Atableview
Commiteditingstyle: (Uitableviewcelleditingstyle) Editingstyle
Forrowatindexpath: (Nsindexpath *) Indexpath
The notification tells the user which cell has been edited, and for the above code, we perform the delete cell operation inside the function.
- -(void) TableView: (UITableView *) Atableview
- Commiteditingstyle: (Uitableviewcelleditingstyle) Editingstyle
- Forrowatindexpath: (Nsindexpath *) Indexpath
- {
- [Chatarray RemoveObjectAtIndex:indexPath.row];
- [Chattableview Reloaddata];
- }
(13) How to get a Cell object for a row
-(UITableViewCell *) Cellforrowatindexpath: (Nsindexpath *) Indexpath;
Summary of basic usage methods for iOS development UITableView