文本未在单元格中显示

时间:2014-05-27 21:09:22

标签: ios objective-c uitableview

尝试让我的应用在其单元格中显示文字。一直在挠我的头。这是用于显示文本但未显示任何内容的代码。有什么建议吗?

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

2 个答案:

答案 0 :(得分:0)

如果您尚未在ViewDidLoad()方法中添加以下代码,请执行此操作。

self.tableView.dataSource = self;
self.tableView.delegate = self;

答案 1 :(得分:0)

首先,你应该以不同方式解决这个问题,这样你就不会在每次制作单元格时重新创建后备存储。

为了确切地确定问题,需要更多代码,但至少您可以将以下内容用于验证此问题。

@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它已经设置了dataSource和委托方法,或者你是否使用了未设置dataSource和delegate的UIViewController(在代码中或通过XIB或Storyboard)?

如果您没有设置委托,则需要通过故事板或代码进行设置,如果通过代码并使用UIViewController确保您有对UITableView的引用,否则您将无法设置数据源。

相关问题