我正在使用本教程介绍如何创建弹出菜单。 http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
我在学习教程时已经开始工作了,现在我正在尝试将它实现到我的应用程序中。
我正处于可以按下菜单按钮并出现弹出菜单的阶段。问题是,它不会用我的表格 View 单元格填充自身。
我为每个表格 View 单元格设置标识符,然后在代码中引用它们来填充一个数组。
我知道在学习本教程时,如果我在定义数组中的内容时拼错了其中一个标识符,程序就会崩溃。在我的应用程序中,情况并非如此。希望这可以帮助解决问题。它甚至不会更改代码第一部分的颜色。
这是代码。
#import "SidebarViewController.h"
#import "SWRevealViewController.h"
@interface SidebarViewController ()
@property (nonatomic, strong) NSArray *menuItems;
@end
@implementation SidebarViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
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];
_menuItems = @[@"markup",@"tax"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
任何帮助都会很棒。
谢谢
最佳答案
您所做的只是创建一个包含两个字符串文字的数组。您必须在以下方法中设置文本标签的 text
属性以在表格 View 单元格中显示字符串:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [_menuItems objectAtIndexPath:indexPath.row];
return cell;
}
关于ios - 我的表格 View 未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047145/