[ios]第二个表不显示在屏幕上

发布时间: 2017/3/28 23:35:03
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有两个 tableviews 在菜单控制器。第一个表填充从 db 的动态菜单列表和第二个表应只显示我告诉它的字符串。所以现在我只需要 2 个细胞,设置和登录。第一个表查看作品很好。但是,第二个不显示的项目。贝娄代表第二个表的代码

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:

你不应该重命名表委托和数据源方法︰ 只是测试传递给他们,以确定他们所涉及的表的表参数。 例如︰

- (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;
    }
}

同样的所有其他表委托和数据源的方法。 你还需要确保委托和数据源设置为两个表的视图。 你可以这样做你 storyboard ,或在代码中 eg。 在 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];
赞助商