Uitabalview Introduction and basic use

Source: Internet
Author: User

1, Uitabalview is a tabular view, indicating: Uitabalview set the data source protocol Uitabalview How to display the data source:-Set the DataSource data source-data source to comply with the Uitabalviewsource protocol- Implement the method in the data source can be//1, Tell TableView section group how many rows-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: ( Nsinteger) Section
{
Xmgcargroup *group = self.groups[section];
return group.cars.count;} 2, tell TableView altogether how many sets of data-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return self.groups.count;} 3, tell TableView Indexpath line shows what kind of cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: ( Nsindexpath *) Indexpath
{UITableViewCell *cell = [[UITableViewCell alloc] init];
Xmgcargroup *group = self.groups[indexpath.section];
Xmgcar *car = Group.cars[indexpath.row];

Cell.textLabel.text = Car.name;
Cell.imageView.image = [UIImage ImageNamed:car.icon];

return cell;} 05-uitableview01-Multi-Group Data 02-refactoring. zip
2.3 MB
2, cell multiplexing The first method of multiplexing://Configure each cell
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
static NSString *identifer = @ "Cellidentifer";
Get from the pool through the identity bit
UITableViewCell *cell = [Tableviewdequeuereusablecellwithidentifier:identifer];
cannot be taken to create
if (!cell) {
cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifer];
} return cell; The second method of multiplexing is to register the cell with identity externally and then use it inside: [_tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "logo"]; -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//     Get UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "logo"] from the pool through the identification bit; return cell;} The third method of multiplexing If TableView is created inside the Main.storyboard, then you can select the cell inside the tableviewcontroll of the Main.storyboard, and then change the cell's identity (which is equivalent to registering the cell in the storyboard), so you can find With the identity of the cell, if we need to add content in the cell, are placed in the Contentview inside, and Cell.textLable.text is actually placed in the contentview inside, just for easy access, Put the textlable on the outside.   If the parent cell in the storyboard, and then modify the identity of the CELL2, is registered two cells, then you can arbitrarily select the two identity of the cell, for example: I want to show the even line display identifies the cell cell2, see    If you use code to represent a cell with two identities registered, you should write this: [_tableview registerclass:[uitableviewcell class]forcellreuseidentifier:@ "Cell"]; [_tableview Registerclass:[uitableviewcell class]forcellreuseidentifier:@ "Cell2"]; Or: static nsstring *identifer = @ "Cell";
static NSString *identifer2 = @ "Cell2";
Get from the pool through the identity bit
UITableViewCell *cell = [Tableviewdequeuereusablecellwithidentifier:identifer];
cannot be taken to create
if (!cell) {
cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifer];
}
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:identifer];
cannot be taken to create
if (!cell) {
cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifer2]; } Proxy protocol method for Uitabalview
    • Agents are used to listen.
When a row is selected, it is called
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (nsindexpath*) Indexpath
{
NSLog (@ "Click--%zd", Indexpath.row);
}
When a row is deselected, it is called
-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) Indexpath
{
NSLog (@ "Click--%zd", Indexpath.row);} Returns the height of each row
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{
if (indexpath.section = = 0) {
Return 44;
}
Return 54;
}//returns the corresponding height of each block head
-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (Nsinteger) section{
return [_headerheightarray[section] floatvalue];
}

Returns the corresponding height for each trailer
-(CGFloat) TableView: (UITableView *) TableView heightforfooterinsection: (Nsinteger) section{
if (section = = 0) {
return 0.1;
}
return 0.1;} Returns a view to the head header, which can be set without height. -(Nullable UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) section{return [UIView *];} Configure the trailer view
-(Nullable UIView *) TableView: (UITableView *) TableView viewforfooterinsection: (Nsinteger) section{
return nil;} Returns the Delete button text that displays each line-(nullable NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) indexpath{
return @ "Delete";} Allow Editing
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (nsindexpath*) indexpath{
return YES;} Return to edit style
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{
return uitableviewcelleditingstyledelete;} Working with edit operations
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (nsindexpath*) indexpath{
Handling Delete Operations
if (Editingstyle = = Uitableviewcelleditingstyledelete) {
Delete data
[Self.alldataarray[indexpath.section]removeobjectatindex:indexpath.row];
Delete cell
[TableView Deleterowsatindexpaths:@[indexpath]withrowanimation:uitableviewrowanimationfade];
}
}//Return header text
-(Nullable NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{
return [NSString stringwithformat:@ "is the head of%ld block", section];
}

Return trailing text
-(Nullable NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) section{return [ NSString stringwithformat:@ "is the tail of%ld block, section];}Extended Knowledge Point: Uitableviewdelegate protocol is inherited from Uiscrollview, so it can also be implementedmethods of uiscrollviewdelegate;such as listeningUITableView's scrollingThis scrollview can also monitor the table movement.
-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{
} errors will be uiviewcontroller as Uitableviewcontroller.What about that? In the Uiviewcontroller, the storyboard removes the UIView, then adds a uitableviewcontroller, and then the class name: the corresponding Tableviewcontroller class. In this case, Self.view and Self.tableview are equivalent. Common settings:1. Set Split Line//To set split line to None
_tableview.separatorstyle =uitableviewcellseparatorstylenone//Set the color of the split line _tableview.separatorcolor = [UIColor Blackcolor];    2. Set the selected background color UIView * Selectedbackgroundview =[[uiviewalloc]init];    Selectedbackgroundview.backgroundcolor = [Uicolor Redcolor]; Set the background color for the cell, because it is set to view, so the configuration can give any views, such as image cell.selectedbackgroundview = Selectedbackgroundview; 3. Set default background color//set default background color
Cell.backgroundcolor = [Uicolor Graycolor];
It also sets the background color, but this point has a higher priority.
UIView * Backgroundview =[[uiview alloc]init];
Backgroundview.backgroundcolor = [Uicolor Redcolor];
Set the background color for the cell, because it is set to view, so the configuration can give any views, such as image cell.backgroundview = Backgroundview; Represents the set indicator, what is an indicator, as in the
It means nothing at all.
Cell.accessorytype = Uitableviewcellaccessorynone; This indicates a tick cell.accessorytype = Uitableviewcellaccessorycheckmark;
This represents the arrow Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; If you want to customize this indicator//customize a view to the indicator above Cell.accessoryview = [[Uiswitch alloc]init]; Custom high-level cell (today encountered a point problem when extracting the corresponding view of tag, [Self.view viewwithtag:1000] This sentence means self.view to call? Now the understanding is: Because tag=1000 corresponds to the view is above the parent view, so use the parent view to take) Evernote 20160520 18:16:36.m4a
68.2 KB

Uitabalview Introduction and basic use

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.