Table control
I. Setting up a data source <UITableViewDataSource>
1. Setting up a data source
-(void) viewdidload { [super Viewdidload]; // set up a data source Self.tableView.dataSource = self ; // set up proxy Self.tableview. delegate = self ; // table head and tail display Self.tableView.tableHeaderView = [[Uiswitch alloc] init]; = [[Uiswitch alloc] init];}
2. How many sets of data are set
/* * * */-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return Self.carsGroup.count; }
3. Set how many rows of data each group has
/* * * */-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ *cargroup = self.carsgroup[section]; return CarGroup.cars.count;}
4. Set the data for each row
/** * Contents of each line*/-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Ldcargroup*cargroup =Self.carsgroup[indexpath.section]; //ID identification of the cache pool StaticNSString *id =@"Car"; //remove the recyclable cell from the cache poolUITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:id]; //There are no recyclable cells in the cache pool if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:nil]; } Ldcar*car =Cargroup.cars[indexpath.row]; //1. Image DisplayCell.imageView.image =[UIImage ImageNamed:car.icon]; //2. Title Text displayCell.textLabel.text =Car.name; //3. Sub-caption DisplayCell.detailTextLabel.text =@"Good!!!"; //4. Set the indicator to the right of the cell /*Uitableviewcellaccessorycheckmark: Display √uitableviewcellaccessorydetailbutton: Show Details button UITABLEVIEWCELLACC Essorydetaildisclosurebutton: Show Detail button + angle bracket uitableviewcellaccessorydisclosureindicator: Show angle brackets UITABLEVIEWCELLACCE Ssorynone: Default Right indicator shows nothing*/ //4.1 Indicators for the systemCell.accessorytype =Uitableviewcellaccessorynone; //4.2 Defining the view display to the right of the cellCell.accessoryview =[[Uiswitch alloc] init]; //5. Set the cell's background color Backgroundview priority > BackgroundColorUiimageview *imageview =[[Uiimageview alloc] init]; Imageview.image= [UIImage imagenamed:@"Btn_left"]; Cell.backgroundview=ImageView; //6. Set the background color when the cell is selectedUiimageview *imageview1 =[[Uiimageview alloc] init]; Imageview1.image= [UIImage imagenamed:@"img_01"]; Cell.selectedbackgroundview=ImageView1; returncell;}
5. Group Header Description
/* * * */-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{ *cargroup = self.carsgroup[section]; return Cargroup.title;}
6. Group Trailer Description
-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) section{ return ; }
7. The right navigation bar of the table shows
/* * * */-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{return [ Self.carsgroup Valueforkeypath:@ "title"];}
Two. Agent Monitoring uitableview<uitableviewdelegate>
1. Monitor which cell is selected
/** * Listen, the cell is selected **/intGroup =0;introw =0;- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Ldcargroup*cargroup =Self.carsgroup[indexpath.section]; Ldcar*car =Cargroup.cars[indexpath.row]; //set up pop-up windowsUialertview *alert = [[Uialertview alloc] Initwithtitle:@"Car Name Display"Message:nilDelegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Confirm", nil]; //set the pop-up window type /*uialertviewstyledefault: Default pop-up window uialertviewstyleloginandpasswordinput: Displays the login dialog box Uialertviewstyleplaintext Input: Display a plain text input box uialertviewstylesecuretextinput: Displays a ciphertext input box*/Alert.alertviewstyle=Uialertviewstyleplaintextinput; //Get a text box that shows the name of the car[Alert Textfieldatindex:0].text =Car.name; //Show pop -up window[Alert show]; //get group number and line numberGroup = (int) indexpath.section; Row= (int) Indexpath.row;}
2. Monitor the Unchecked cell
/* * * */-(void) TableView: (UITableView *) TableView Didenddisplayingheaderview: (UIView *) view Forsection: (Nsinteger) section{ }
Three. Agent monitoring Uialertview pop-up window <UIAlertViewDelegate>
1. Monitor click on which button on the Uialertview pop-up window
** *Monitor clicked on which button on the Uialertview pop-up window.*/- (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{//1. If you click the Cancel button on the left, directly put it back if(Buttonindex = =0)return; //2. Get the last text in the TextBoxNSString *name = [Alertview textfieldatindex:0].text; //3. Modifying model dataLdcargroup *cargroup =Self.carsgroup[group]; Ldcar*car =Cargroup.cars[row]; Car.name=name; //4. Refresh the data//4.1 Global Refresh Data[Self.tableview Reloaddata]; //4.2 Local Refresh Data /*uitableviewrowanimationfade: Flash Uitableviewrowanimationright:cell swipe out from the left to refresh Uitableviewrowanimationlef T:cell slide from right to left to refresh Uitableviewrowanimationtop:cell swipe out from bottom to refresh Uitableviewrowanimationbottom:uitableviewrowanim Ationnone: No effect Uitableviewrowanimationmiddle:cell swipe out from top to refresh uitableviewrowanimationautomatic: animation flashes*/Nsindexpath*path =[Nsindexpath Indexpathforrow:row Insection:group]; [Self.tableview Reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationautomatic];}
Basic use of iOS development UI UITableView