ios - 一个 View Controller 中的两个表

标签 ios objective-c uitableview

我在一个 viewController 和一个菜单按钮中有两个 TableView 。 最初只显示 tableView1,当我按下菜单按钮时,第二个 TableView 应该出现并且 tableView1 仍然存在。

我阅读并实现了我发现的内容,但没有结果。

两个 TableView 都出现了,但是数据是一样的,我不想这样。我试图这样做:

- (void) viewDidLoad {
    tableView.hidden = NO;
    tableViewMenu.hidden = YES;

    tableView.delegate = self;
    tableView.dataSource = self;
    tableViewMenu.delegate = self;
    tableViewMenu.dataSource = self;    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == tableView) {
        return 30;
    }        
    else { 
        return 4;
    }        
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == tableView) {
        static NSString *CellIdentifier1 = @"Cell1";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
        }

        cell.textLabel.text = @"Table 1";
        NSLog(@"1here is%i  %@",indexPath.row,cell.textLabel.text);
        return cell;
    } else {

        static NSString *CellIdentifier2 = @"Cell2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
        }

        cell.textLabel.text = @"Table 2";
        return cell;
    }
}


int i = 1;
- (void) showMenu {
    //slide the content view to the right to reveal the menu
    //tableView.hidden = NO;
    tableViewMenu.hidden = NO;

    NSLog(@"Display");

    if (i == 1) {
        [UITableView animateWithDuration:.25
                              animations:^{
                                  [tableView setFrame:CGRectMake(tableViewMenu.frame.size.width, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height)];
                                  [testView setFrame:CGRectMake(tableViewMenu.frame.size.width, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height)];

         }];    
         i = 0;        

    } else {          
        [UIView animateWithDuration:.25
                         animations:^{
                             [tableView setFrame:CGRectMake(0, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height)];
                             [testView setFrame:CGRectMake(0, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height)];
                             tableViewMenu.hidden=YES;

        }];
        i = 1;
    }
}

最佳答案

我想问题出在这条线上:

if(tableView==tableView)

您应该将 tableview 参数与您的 @property 进行比较。 如果参数和 iVar 具有相同的名称,则参数将覆盖方法范围内的 iVar。

if (tableview == self.tableView)

关于ios - 一个 View Controller 中的两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28495997/

相关文章:

ios - 删除开发中使用的 CoreData 迁移

objective-c - CALAyer 子类中的不可动画属性

ios - 如何知道拖放结束时的最后一个 x 和 y 值?

ios - 将底部单元格添加到动态表格 View

ios - Swift:如何防止未显示的 UITableViewCells 在滚动时被关闭

javascript - iOS 9 上的 Safari 不会触发隐藏输入文件的点击事件

ios - 没有这样的模块 'AWSCore'

ios - 核心数据 : `EXC_BAD_ACCESS` When Accessing Objects in Relationship

ios - 在什么情况下运行时可能会在 iOS 中生成一个排序的方法列表

ios - NSMutableString 追加后为空