[英]Two Tableviews in One view controller and One tableview not working when setting DS for 2nd one
我在一个视图控制器中有两个tableviews。 一个在界面生成器中,另一个是我使用viewDidLoad()方法中的以下代码动态创建的。
// Creating the view for placing the dynamic tableview
-(UIView *) createAndAddMenuView :(float) viewHeight
{
UIView *myView = [[UIView alloc] init];
myView.backgroundColor = [UIColor blueColor];
CGRect coord = myView.frame;
coord.origin.x = -255;
coord.origin.y = 0;
coord.size.width = 255;
coord.size.height = viewHeight;
[myView setFrame:coord];
return myView;
}
-(void) addMenuItemsTable{
UITableView *dynamicTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 255, self.navigationController.view.frame.size.height) style:UITableViewStylePlain];
dynamicTable.delegate=self;
dynamicTable.dataSource=self;
dynamicTable.tag = 20;
//[dynamicTable reloadData];
[menuView addSubview:dynamicTable];
}
这两个表视图都将委托和数据源设置为self。 将第二个动态tableview添加到视图中,并以x = -255放置在左侧的隐藏侧。 当我单击导航栏中的按钮时,我将“ menuView”视图移到屏幕上,并将其他静态tableview移出屏幕,就像Facebook应用程序一样。
我正在使用此代码来回移动menuView。
-(void) toggleMainView :(UITableView *) mytableView withMenuView : (UIView * )menuView{
NSLog(@"TAG %d",mytableView.tag);
CGRect destination = mytableView.frame;
NSLog(@"XX %f",destination.origin.x);
if (destination.origin.x > 0) {
destination.origin.x = 0;
} else {
destination.origin.x += 255;
}
NSLog(@"ORG X = %f", destination.origin.x );
[UIView animateWithDuration:0.25 animations:^{
[self showMenu:menuView];
mytableView.frame = destination;
} completion:^(BOOL finished) {
mytableView.userInteractionEnabled = !(destination.origin.x > 0);
}];
}
-(void)showMenu :(UIView *)menuView{
CGRect coord = menuView.frame;
NSLog(@"Width = %f, x = %f",coord.size.width, coord.origin.x);
if(coord.origin.x < 0){
coord.origin.x = 0;
}else{
coord.origin.x = -255;
}
[menuView setFrame:coord];
}
但是,当我没有设置第二个tableview数据源时,仅此代码有效。 我不知道为什么会这样。
即。 当我注释掉这一行
dynamicTable.dataSource = self;
然后,只有当我单击按钮时,第一个表格视图才会移出屏幕。 所有这些时间,动态的都会在屏幕上来回移动。 如果不评论DS,则第一个(静态表格视图)不会移动,而第二个(动态表格视图)将移动。
这是我的第一个iPhone应用程序。
如果您使用的是2个tableview或1个tableview,请尝试使用两个不同的值为其设置标签值,假设table1标签为50,table2标签为60,则tableview委托和数据源中的代码应如下所示。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.tag==50) // if you have 2 different objects for tableviews then you can also use like this if(tableView == tableviewObjc1)
{
// tableview1 info
}
else
{
// tableview2 info
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
if(tableView.tag==50)
{
// tableview1 info
}
else
{
// tableview2 info
}
return cell;
}
您可以使用任何可满足您cocoacontrols要求的自定义控件,其中之一是
MVYSideMenu
您不需要在一个ViewController中添加两个tableview
您应该尝试以下方法:
脚步:
将两个原型单元格添加到Storybaord
制作两个自定义单元类来继承UITableViewCell并设计单元情节提要或通过代码
cellForRowAtIndexPath:根据要求初始化单元格,并将其设置为数据源并相应地委托方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.