The UITableView of iOS development

Source: Internet
Author: User


Age: Confidentiality

Sex: Straight Guy

Character: Low-key and steady, perverse connotation

Blog background: has been using UITableView, but has always been only the first knowledge, and later in the use of constantly found a lot of things not cared about, then sorted out, of course, there are a lot of people in the blog to see the points mentioned, I put him on a heavy step again;

1. Click to display the selected state, but the status of the village, you must click on the next time to uncheck the state

when clicking on the cell, call -(void) TableView: (UITableView) TableView Didselectrowatindexpath: (Nsindexpath) Indexpath;
Call away when clicked,
-(void) Deselectrowatindexpath: (Nsindexpath *) Indexpath animated: (BOOL) animated;

The general usage is to add [TableView Deselectrowatindexpath:indexpath Animated:yes] to the Didselectrowatindexpath method, i.e. the cell has a background color when clicked on the cell. If you have not selected another, then this cell background color has been, adding this sentence effect is after clicking the end of the cell background color disappears.

-(void) TableView: (UITableView) TableView Diddeselectrowatindexpath: (nsindexpath) Indexpath2.

2.cell background color When selected (default gray, now seems to be only colorless and gray two types of)

@property (nonatomic) Uitableviewcellselectionstyle Selectionstyle;





Important attributes:

Indexpath.row: line indexpath.section: Group

Separatorcolor: Color of Split line Separatorstyle: Split line Style

Custom view with custom header and footer only needs to set the height, the width setting is invalid



The data presented in table is obtained from the model (or other source-data source), so to change the tabular data, you only need to change the data in the source and refresh the table. (Do not directly change the text in the Textlabel in Tableviewcell, so drag tableview up or down to show the original data)

Common Properties of UITableViewCell

Accessorytype:cell indicator to the right

Accessoryview: Customizable to the right view

Backgroundview: Custom Background view

backgroundcolor//priority is lower than backgroundview


3. Display Right Index

- (Nsarray) tableview{nsmutablearray *indexs = [[Nsmutablearray alloc] init];   for (int i = 0; i < kheadertitle.count; i++) {[Indexs Addobject:khead        Ertitle[i]]; }  return  indexs;}

4. Delete operation

in general, if you swipe the right side of the cell, the delete button will be deleted. In fact, the implementation of this function as long as the implementation of proxy method, as long as the implementation of this method to the left will show the delete button. Just click the Delete button and this method will be called. -(void) TableView: (UITableView) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath) Indexpath;
- (void) TableView: (UITableView) indexpath{if(Editingstyle = =uitableviewcelleditingstyledelete)                    {[_titlearray removeobject:_titlearray[indexpath.row]];             [TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationbottom]; } }

5. The navigation bar picture is stretched to enlarge

 onor create a controller, inside the controller to create TableView, initialize its necessary proxy method to enable its normal display.
Genevawhen initializing the TableView, let the tableView shift downward (the part of the image that is shifted down): _tableview.contentinset= Uiedgeinsetsmake (Backgroupheight- -,0,0,0);
GenevaInitialize the picture, note the frame settings of the picture, load it on the TableView IMAGEBG= [[Uiimageview alloc] Initwithframe:cgrectmake (0,                    -backgroupheight, Kdevicewidth, Backgroupheight)]; Imagebg.image= [UIImage imagenamed:@"Bg_header.png"]; [_tableview ADDSUBVIEW:IMAGEBG];
GenevaChanges the frame of the picture according to the offset of the slide, changing the transparency of the Navigationbar- (void) Scrollviewdidscroll: (uiscrollview) scrollview{cgfloat yoffset=Scrollview.contentoffset.y; CGFloat Xoffset= (Yoffset + backgroupheight)/2; if(Yoffset <-backgroupheight) {CGRect rect=Imagebg.frame; RECT.ORIGIN.Y=Yoffset; Rect.size.height= -Yoffset; Rect.origin.x=Xoffset; Rect.size.width= Kdevicewidth + fabs (xoffset)2; Imagebg.frame=rect; } cgfloat Alpha= (Yoffset + backgroupheight)/Backgroupheight; [Self.navigationController.navigationBar setbackgroundimage: [self imagewithcolor:[[uicolor ORANGECOLOR] Colo  Rwithalphacomponent:alpha]] forbarmetrics:uibarmetricsdefault]; Titlelb.textcolor= [Uicolor colorwithred:255Green255Blue255Alpha:alpha];}
toRender Navigationbar Color method-(UIImage) Imagewithcolor: (uicolor) color{//Description RectangleCGRect rect = CGRectMake (0.0f,0.0f,1.0f,1.0f); //Open Bitmap ContextUigraphicsbeginimagecontext (rect.size); //Get Bitmap ContextCgcontextref content =Uigraphicsgetcurrentcontext (); //populating a context with a color presentationCgcontextsetfillcolorwithcolor (content, [color Cgcolor]); //Render Contextcgcontextfillrect (content, rect); //get a picture from the contextUIImage *currentimage =Uigraphicsgetimagefromcurrentimagecontext (); //End ContextUigraphicsendimagecontext (); returncurrentimage;}

6. Refresh the interface

No. 1
. Refresh the entire Tableview[self.tableview reloaddata];? Note: Here Reloaddata is refreshing the entire uitableview, and sometimes we may need to refresh it locally. For example: Just refresh a cell, refresh only one section, and so on. This time in the call Reloaddata method, although the user can not see, but some waste resources.2
Refresh local Cellnsindexpath*indexpath = [Nsindexpath indexpathforrow:0Insection:0]; [Self.tableviewreloadrowsatindexpaths:[nsarray arraywithobjects:indexpath,nil]withrowanimation: Uitableviewrowanimationfade]; Note: This is a convenient way to refresh the first cell of the first section, although it seems that the code is much more, but it saves resources and minimizes the refresh rate. This is also an optimization of UITableView in iOS development.
NO.3Local Refresh Sectionnsindexset*indexset = [[Nsindexset alloc] Initwithindex:0]; [Self.tableview ReloadSections:indexSetwithRowAnimation:UITableViewRowAnimationFade]; The above code is a No. 0 section refresh.
NO.4Refresh Animation Refresh UITableView There are several animations: typedef ns_enum (Nsinteger, uitableviewrowanimation) {uitableviewrowanimationfade, //fade in and fadeUitableviewrowanimationright,//swipe in from right//Slide in from right (or out to right)Uitableviewrowanimationleft,//swipe in from leftUitableviewrowanimationtop,//swipe in from topUitableviewrowanimationbottom,//from slipping intoUitableviewrowanimationnone,//available in IOS 3.0Uitableviewrowanimationmiddle,//available in IOS 3.2. Attempts to keep cells centered in the space it will/did occupyUitableviewrowanimationautomatic = -//available in IOS 5.0. Chooses a appropriate animation style for you};

The UITableView of iOS development

Related Article

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: 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.