在为第二个设置DS时,一个视图控制器和一个tableview中的两个Tableview无效

时间:2014-03-04 03:11:35

标签: ios iphone

我在一个视图控制器中有两个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];
}

这两个tableviews都将委托和数据源设置为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 Datasource时,只有这个代码正常工作。 我不知道为什么会这样。

即。当我注释掉这一行时

dynamicTable.dataSource =自我;

然后,只有当我点击按钮时,第一个桌面视图才会移出屏幕。 所有这些时候,动态的将在屏幕中来回移动。 当DS没有被注释时,第一个(静态tableview)将不会移动而第二个(动态一个)将移动。

这是我的第一个iPhone应用程序。

2 个答案:

答案 0 :(得分:0)

如果您正在使用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;
}

答案 1 :(得分:0)

您可以使用任何适合您要求的自定义控件 来自cocoacontrols其中一个是

MVYSideMenu
您不需要在一个ViewController中添加两个tableview

您应该尝试以下方法:

<强>步骤进行:

  1. 将两个原型单元添加到storybaord

  2. 制作两个自定义单元类子类UITableViewCell并设计您的单元格故事板或通过代码

  3. cellForRowAtIndexPath:根据需要初始化单元格并设置数据源并相应地委托方法

相关问题