Ios 未在单元格中显示的文本
正在尝试让我的应用程序在其单元格中显示文本。我挠头有一阵子了。这是用于显示文本的代码,但没有显示任何内容。有什么建议吗Ios 未在单元格中显示的文本,ios,objective-c,uitableview,Ios,Objective C,Uitableview,正在尝试让我的应用程序在其单元格中显示文本。我挠头有一阵子了。这是用于显示文本的代码,但没有显示任何内容。有什么建议吗 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"leftMenuCell"; UITableViewCell *cell =
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
NSArray *titles = @[@"Quick Glance", @"Your Home", @"Invites"];
cell.textLabel.text = titles[indexPath.row];
}
cell.backgroundColor = [UIColor clearColor];
return cell;
}
如果尚未在ViewDidLoad()方法中添加以下代码,请执行此操作
self.tableView.dataSource = self;
self.tableView.delegate = self;
首先,您应该以不同的方式进行分解,这样您就不会在每次生成单元时都重新创建备份存储 需要更多的代码来准确确定问题所在,但至少您可以输入以下代码来验证这一点
@implementation ViewController {
NSArray *_titles;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_titles = @[@"Quick Glance", @"Your Home", @"Invites"];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = titles[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
这是在tableview中显示某些信息所需的最低限度
现在的问题是,您使用的是已经设置了数据源和委托方法的UITableViewController,还是尚未设置数据源和委托的UIViewController(在代码中或通过XIB或故事板)
如果没有设置代理,则需要通过故事板或代码设置代理,如果通过代码并使用UIViewController,请确保您有一个对UITableView的引用,否则您将无法设置数据源。请看一看,看看它是否有帮助。问题不在您发布的代码中。您是否为tableView连接了数据源?顺便说一句:您正在使用旧的API。在iOS 5及更高版本中,您不需要检查单元格是否为nil,因为它是自动为您创建的。@KaiEngelhardt只有在您使用已注册的单元格类或nib文件(故事板中的原型单元格隐式添加为已注册的nib)时才是如此。@David您是对的,我忘记了:D