gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:32:01 26 4
gpt4 key购买 nike

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

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

有什么问题吗?

最佳答案

您不应该重命名 tableView 委托(delegate)和数据源方法:只需测试传递给它们的 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 委托(delegate)和数据源方法。您还需要确保为两个 TableView 都设置了委托(delegate)和数据源。您可以在 Storyboard 中或在代码中执行此操作,例如。在 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];

关于ios - 第二个 TableView 没有显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com