第二个TableView没有显示在屏幕上

时间:2014-12-10 19:51:14

标签: ios objective-c xcode

我在菜单控制器中有两个tableviews。第一个tableview从db填充动态菜单列表,第二个tableview应该只显示我告诉它的字符串。所以现在我只需要2个单元格,设置和登录。第一个表视图工作正常。但是,第二个不显示项目。代码bellow代表第二个tableview

viewDidLoad中

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.slidingViewController setAnchorRightRevealAmount:280.0f];
    self.slidingViewController.underLeftWidthLayout = ECFullWidth;
    self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
    self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
}

主表

-(void)setMenuItems:(NSArray *)menuItems
{
    if(_menuItems != menuItems)
    {
        _menuItems = menuItems;
    }
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
     return self.menuItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"MenuItemCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    Department *dept = [self.menuItems objectAtIndex:indexPath.row];

    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = dept.name;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];

    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    myBackView.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = myBackView;
    return cell;
}

第二张表

-(void)setExtraMenuItems:(NSArray *)extraMenuItems
{
    if(_extraMenuItems != extraMenuItems)
    {
        _extraMenuItems = extraMenuItems;
    }
    [self.extraTableView reloadData];
}    

- (NSInteger)extraTableView:(UITableView *)extraTableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return self.extraMenuItems.count;
}

- (UITableViewCell *)extraTableView:(UITableView *)extraTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Formal";

    UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    [_extraMenuItemFiller addObject:@"Settings"];
    [_extraMenuItemFiller addObject:@"Logout"];

    NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];

    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = cellValue;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];

    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    myBackView.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = myBackView;

    return cell;
}

它出了什么问题?

1 个答案:

答案 0 :(得分:1)

您不应该重命名tableView委托和数据源方法:只需测试传递给它们的tableView参数,以确定它们与哪个tableView相关。例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    if (tableView == self.extraTableView) {
        return self.extraMenuItems.count;
    } else {
        return self.menuItems.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.extraTableView) {
        NSString *CellIdentifier = @"Formal";

        UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        [_extraMenuItemFiller addObject:@"Settings"];
        [_extraMenuItemFiller addObject:@"Logout"];

        NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];

        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.text = cellValue;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];

        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = myBackView;

        return cell;
    } else {
        NSString *cellIdentifier = @"MenuItemCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        }

        Department *dept = [self.menuItems objectAtIndex:indexPath.row];

        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.text = dept.name;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];

        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = myBackView;
        return cell;
    }
}

同样适用于所有其他tableView委托和数据源方法。您还需要确保为两个表视图设置了委托和数据源。您可以在故事板或代码中执行此操作。在viewDidLoad

self.extraTableView.delegate = self;
self.extraTableView.datasource = self;

修改

您不需要extraMenuItems和extraMenuItemFiller。我会使用extraMenuItems。使用viewDidLoad中的两个值加载它,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.slidingViewController setAnchorRightRevealAmount:280.0f];
    self.slidingViewController.underLeftWidthLayout = ECFullWidth;
    self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
    self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
    self.extraMenuItems = @[@"Login",@"Settings"];
    self.extraTableView.delegate = self;
    self.extraTableView.datasource = self;
}

并修改cellForRowAtIndexPath以使用extraMenuItems而不是extraMenuItemFiller:

    NSString *cellValue = [self.extraMenuItems objectAtIndex:indexPath.row];