A page displays two TableView, and the data on each tableview is different, typically in the following ways:
Start by building a class that inherits from UIView to represent the view used to toggle TableView
//In the. h file of the view's class
#import <UIKit/UIKit.h>
@protocol myattentionheadviewdelegate <NSObject> //Build a proxy called Myattentionheadviewdelegate
@optional //Two agent methods (optional implementation of optional)
-(void) myattentionheadviewleftbtnclicked: (uibutton*) btn;
-(void) myattentionheadviewrightbtnclicked: (uibutton*) btn;
@end
@interface Myattentionheadview:uiview
@property (Nonatomic,strong) UIButton *leftbtn; //define left button
@property (Nonatomic,strong) UIButton *rightbtn; //Define right button
@property (Nonatomic,strong) UIView *colorview; //define the color bar below the button
@property (nonatomic,assign) id<myattentionheadviewdelegate>delegate; //Agent
@end
//In the. m file of the view's class
#import "MyAttentionHeadView.h"
@implementation Myattentionheadview
The initWithFrame method is used to initialize and return a new View object, based on the specified cgrect (size)
-(ID) initWithFrame: (CGRect) frame{
Self =[super initwithframe:frame];
if (self) {
Self.backgroundcolor =[uicolor Whitecolor]; //Set the background color of the view
_colorview =[[uiview Alloc]init]; //Create button below color bar
_colorview.frame =cgrectmake (self.frame.size.width/2.0, self.frame.size.height-1.0, self.frame.size.width/2.0, 1.0 );
_colorview.backgroundcolor =[uicolor Greencolor];
[Self addsubview:_colorview]; //Add a color bar to the view
_LEFTBTN =[uibutton Buttonwithtype:uibuttontypecustom]; //Create left button
[_leftbtn settitle:@ "interested buyers" forstate:uicontrolstatenormal]; //Set the text on the button
[_leftbtn Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal]; //Set text color in no selected state
[_leftbtn Settitlecolor:[uicolor Greencolor] forstate:uicontrolstateselected]; //Set the text color in select State
_leftbtn.titlelabel.font =[uifont systemfontofsize:15.0f]; //Set Text size
_leftbtn.frame =cgrectmake (0, 0, self.frame.size.width/2.0, self.frame.size.height-1.0); //Set the frame of the button
[_leftbtn addtarget:self Action: @selector (leftbtnclicked:) forcontrolevents:uicontroleventtouchupinside];
//Add a Click event for the button
_leftbtn.backgroundcolor =[uicolor Whitecolor]; //Set button background color
[Self addsubview:_leftbtn]; //Add a button to the view
_RIGHTBTN =[uibutton Buttonwithtype:uibuttontypecustom];
_rightbtn.frame =cgrectmake (self.frame.size.width/2.0, 0, self.frame.size.width/2.0, self.frame.size.height-1.0);
_rightbtn.backgroundcolor =[uicolor Whitecolor];
[_rightbtn settitle:@ "farm of Concern" forstate:uicontrolstatenormal];
[_rightbtn Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal];
[_rightbtn Settitlecolor:[uicolor Greencolor] forstate:uicontrolstateselected];
_rightbtn.titlelabel.font =[uifont systemfontofsize:15.0f];
[_rightbtn addtarget:self Action: @selector (rightbtnclicked:) forcontrolevents:uicontroleventtouchupinside];
[Self addsubview:_rightbtn];
}
return self;
}
Click the Left button event
-(void) leftbtnclicked: (uibutton*) btn{
_leftbtn.selected =yes; //Left button selected State
_rightbtn.selected =no; //Right button unchecked
//Click the button to animate the settings
[UIView animatewithduration:0.2f animations:^{
_colorview.frame =cgrectmake (0, self.frame.size.height-1.0, self.frame.size.width/2.0, 1.0);
//color bar below the left button
}];
//If there is a method called myattentionheadviewleftbtnclicked:, then call this method
if ([_delegate respondstoselector: @selector (myattentionheadviewleftbtnclicked:)]) {
[_delegate MYATTENTIONHEADVIEWLEFTBTNCLICKED:BTN];
}
}
//Click on the right button event
-(void) rightbtnclicked: (uibutton*) btn{
_leftbtn.selected =no;
_rightbtn.selected =yes;
[UIView animatewithduration:0.2f animations:^{
_colorview.frame =cgrectmake (self.frame.size.width/2.0, self.frame.size.height-1.0, self.frame.size.width/2.0, 1.0 );
}];
if ([_delegate respondstoselector: @selector (myattentionheadviewrightbtnclicked:)]) {
[_delegate MYATTENTIONHEADVIEWRIGHTBTNCLICKED:BTN];
}
}
@end
Import the header file of the view class in the. m file of the Controller class
@interface Myattentionviewcontroller () <uitableviewdatasource,uitableviewdelegate,myattentionheadviewdelegate >
Compliance with TableView's two agents and custom proxies in the view class
@property (Weak, nonatomic) Iboutlet UITableView *lefttableview; //Drag two tableview into the Xib file
@property (Weak, nonatomic) Iboutlet UITableView *righttableview;
@property (Nonatomic,strong) Myattentionheadview *headview; //Define a view's properties
@property (Nonatomic,strong) Nsmutablearray *arrays; //Data source
@end
@implementation Myattentionviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Self.navigationItem.title [email protected] "my concern"; //Set title
_headview =[[myattentionheadview alloc]initwithframe:cgrectmake (0, Self.view.frame.size.width, 40)]; //instantiation of Headview
_headview.delegate =self; //Set up proxy
[Self.view Addsubview:_headview]; //Add Headview to TableView
[Self refreshdata]; //Refresh Data
}
//Refresh Data method
-(void) refreshdata{
//Create false data for the data source to display the number of cells
_arrays =[nsmutablearray Arraywithcapacity:2];
Nsarray *array1 =[nsmutablearray arraywitharray:@[@ "0"];
Nsarray *array2 =[nsmutablearray arraywitharray:@[@ "0", @ "1", @ "2", @ "3", @ "4", @ "5"];
[_arrays Addobject:array1];
[_arrays Addobject:array2];
[Self.lefttableview Reloaddata]; //tableview Refresh
[Self.righttableview Reloaddata];
}
-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{
return _arrays.count; //How many groups
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
Nsarray *ary =_arrays[section];
return ary.count; //number of rows per group
}
-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
if ([TableView Isequal:_lefttableview]) {
//If the left TableView is displayed
if (indexpath.section==0 && indexpath.row==0) {
//First Group first line load Addnewpurchasecell
Addnewpurchasecell *cell =[tableview dequeuereusablecellwithidentifier:@ "Addnewpurchasecell"];
if (Cell==nil) {
Cell =[[[nsbundle mainbundle]loadnibnamed:@ "Addnewpurchasecell" Owner:nil Options:nil]lastobject];
}
return cell;
}else{
//Otherwise load Attentionpurchasecell
Attentionpurchasecell *cell1 =[tableview dequeuereusablecellwithidentifier:@ "Attentionpurchasecell"];
if (Cell1==nil) {
Cell1 =[[[nsbundle mainbundle]loadnibnamed:@ "Attentionpurchasecell" Owner:nil Options:nil]lastobject];
// Note: don't forget to write Lastobject
}
return cell1;
}
}else if ([TableView Isequal:_righttableview]) {
//If the right TableView is displayed
if (indexpath.section==0 && indexpath.row ==0) {
Addnewfarmcell *cell2 =[tableview dequeuereusablecellwithidentifier:@ "Addnewfarmcell"];
if (Cell2==nil) {
Cell2 =[[[nsbundle mainbundle]loadnibnamed:@ "Addnewfarmcell" Owner:nil Options:nil]lastobject];
}
return cell2;
}else{
Attentionfarmcell *cell3 =[tableview dequeuereusablecellwithidentifier:@ "Attentionfarmcell"];
if (Cell3==nil) {
Cell3 =[[[nsbundle mainbundle]loadnibnamed:@ "Attentionfarmcell" Owner:nil Options:nil]lastobject];
}
return cell3;
}
}
return nil;
}
Determine if TableView can be edited
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{
return indexpath.section==0 No:yes;
}
Judging edit Type
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{
return uitableviewcelleditingstyledelete;
}
The deletion of English into Chinese
-(nsstring*) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) indexpath{
return @ "Delete";
}
Delete method
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{
if (Editingstyle ==uitableviewcelleditingstyledelete) {
//If the type of edit is delete, select a row to delete
Nsmutablearray *ARRM =_arrays[indexpath.section];
NSObject *obj =arrm[indexpath.row];
if ([Arrm containsobject:obj]) {
[Arrm Removeobject:obj];
}
}
[_lefttableview Reloaddata]; //Refresh two x TableView
[_righttableview Reloaddata];
}
The implementation of two custom proxy methods
-(void) myattentionheadviewleftbtnclicked: (uibutton*) btn{
_lefttableview.hidden=no;
_righttableview.hidden=yes;
}
-(void) myattentionheadviewrightbtnclicked: (uibutton*) btn{
_lefttableview.hidden =yes;
_righttableview.hidden =no;
}
One page in iOS shows two tableview cases