【问题标题】:Two Tableviews in One view controller and One tableview not working when setting DS for 2nd one将 DS 设置为第二个时,一个视图控制器中的两个表视图和一个表视图不起作用
【发布时间】:2014-03-04 15:26:44
【问题描述】:

我在一个视图控制器中有两个表视图。 一个在界面生成器中,另一个是我使用 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。第二个动态表格视图被添加到视图中,并放置在左侧隐藏侧,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;

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

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

【问题讨论】:

  • 请详细说明“但是当我没有设置第二个 tableview 数据源时,只有这个代码在工作”
  • @mackworth :- 我已经编辑了帖子以添加更多解释
  • @Vipin 你的解释不够详细。请添加完整的源代码或尝试更好地解释您的问题。
  • 您的问题解决了吗
  • @Charan Giri :- 不,请帮忙。我陷入了困境。

标签: ios iphone


【解决方案1】:

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

【讨论】:

  • :- 这与设置值无关。动画没有发生。
【解决方案2】:

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

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

您应该尝试以下方法:

步骤:

  1. 将两个原型单元添加到故事板

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

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

【讨论】:

  • 他正在寻找滑动动画,就像 facebook 应用程序一样,菜单从左侧滑入并将数据推送到右侧。
  • 是的,我只是在寻找动画。当我放置两个 Tableview 并为自己设置委托和 ds 时,它不会滑动..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
相关资源
最近更新 更多