Ios achieves two tableview linkages and iostableview linkages
Linkage between two tableviews, sliding the left tableview, and sliding the right tableview
In fact, the implementation is relatively simple, but we only need to clarify the differences and connections between them, and call
-(Void) tableView :( UITableView *) tableView willDisplayHeaderView :( UIView *) view forSection :( NSInteger) section
This method achieves the association between the left and right tableviews.
Directly Add code
@ Implementation ViewController
{
UITableView * _ rightTableView;
UITableView * _ leftTableView;
NSArray * _ leftTableSource;
NSArray * _ rightTableSource;
}
Initialize the data source
-(Void) viewDidLoad {
[SuperviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_ LeftTableSource = @ [@ "11", @ "22", @ "33", @ "44", @ "55", @ "66"];
_ RightTableSource = @ [@ {@ "header": @ "11", @ "title": @ [@ "aa", @ "bb", @ "cc ", @ "dd", @ "ee", @ "ff"]},
@ {@ "Header": @ "22", @ "title": @ [@ "gg", @ "mm", @ "nn", @ "oo ", @ "pp", @ "qq"]},
@ {@ "Header": @ "33", @ "title": @ [@ "rr", @ "ss", @ "jj", @ "xx ", @ "yy", @ "zz"]},
@ {@ "Header": @ "44", @ "title": @ [@ "aa", @ "bb", @ "cc", @ "dd ", @ "ee", @ "ff"]},
@ {@ "Header": @ "55", @ "title": @ [@ "gg", @ "mm", @ "nn", @ "oo ", @ "pp", @ "qq"]},
@ {@ "Header": @ "66", @ "title": @ [@ "rr", @ "ss", @ "jj", @ "xx ", @ "yy", @ "zz"]}];
[SelfsetupSomeParamars];
}
Create two tableviews
-(Void) setupSomeParamars
{
_ RightTableView = [[UITableViewalloc] initWithFrame: CGRectMake (100, 0, self. view. frame. size. width-, self. view. frame. size. height) style: UITableViewStyleGrouped];
_ RightTableView. dataSource = self;
_ RightTableView. delegate = self;
[Self. viewaddSubview: _ rightTableView];
_ LeftTableView = [[UITableViewalloc] initWithFrame: CGRectMake (100, self. view. frame. size. height) style: UITableViewStyleGrouped];
_ LeftTableView. dataSource = self;
_ LeftTableView. delegate = self;
[Self. viewaddSubview: _ leftTableView];
}
Set cell display
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * reuseIdentifer = @ "cell ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: reuseIdentifer];
If (! Cell ){
Cell = [[UITableViewCellalloc] initWithStyle: UITableViewCellStyleDefaultreuseIdentifier: reuseIdentifer];
}
If (tableView = _ rightTableView ){
Cell. textLabel. text = [_ rightTableSource [indexPath. section] objectForKey: @ "title"] [indexPath. row];
} Elseif (tableView ==_lefttableview ){
Cell. textLabel. text = _ leftTableSource [indexPath. row];
}
Return cell;
}
-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section
{
If (tableView = _ rightTableView ){
Return 50;
} Else {
Return 50;
}
}
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
{
If (tableView = _ rightTableView ){
Return_rightTableSource.count;
} Else {
Return 1;
}
}
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
If (tableView = _ leftTableView ){
Return_leftTableSource.count;
} Else {
Return [[_ rightTableSource [section] objectForKey: @ "title"] count];
}
}
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section
{
If (tableView = _ rightTableView ){
Return [_ rightTableSource [section] objectForKey: @ "header"];
} Else {
Return nil;
}
}
-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section
{
If (tableView = _ rightTableView ){
UILabel * label = [[UILabelalloc] initWithFrame: CGRectMake (100, self. view. frame. size. width-, 40)];
Label. backgroundColor = [UIColorcyanColor];
Label. text = [_ rightTableSource [section] objectForKey: @ "header"];
Label. textColor = [UIColorredColor];
Return label;
} Else {
Return nil;
}
}
The linkage effect is here
-(Void) tableView :( UITableView *) tableView willDisplayHeaderView :( UIView *) view forSection :( NSInteger) section
{
If (tableView = _ rightTableView ){
[_ LeftTableViewselectRowAtIndexPath: [NSIndexPathindexPathForItem: section inSection: 0] animated: YESscrollPosition: UITableViewScrollPositionNone];
}
}
For more information, see the demo. However, there are some shortcomings.