ios - 我的表格 View 未填充

标签 ios objective-c uitableview

我正在使用本教程介绍如何创建弹出菜单。 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/

相关文章:

ios - 在静态表格 View 中更改单元格的选择颜色

ios - UINavigationBar 设置背景,并设置半透明不起作用

ios - ADALiOS-如何以静默方式刷新accessToken?

ios - 如何通过 xcode 中的 nib 将 UIViewController 放置在另一个 UIViewController 中

objective-c - iOS 5 中的 setTitleView 奇怪行为 - objective-c

c# - Xamarin 如何根据默认设置以编程方式选择uitableview单元格

ios - UITableView 使用 UIViewController 不加载 tableView

objective-c - 我怎样才能使这段代码更有效率( Objective-C )?

objective-c - 如何复制CGLayer

iphone - 从其他控件继承的值