【问题标题】:UITableViewCell textLabel not displaying textUITableViewCell textLabel 不显示文本
【发布时间】:2014-09-09 00:43:58
【问题描述】:

我试图让我的 UITableViewCell 在其 textLabel 中显示正确的文本,但到目前为止没有显示任何文本。我试图对数组进行 NSLOG 以确保它有数据并且确实有。我尝试遍历数组并从我正在获取的核心数据实体中的每个条目中打印文本,然后我得到了我想要的文本。在单元格中显示所有这些信息时,什么也没有发生。我还注意到 cellForRowAtIndexPath 方法也没有被调用。

首先是从 UIAlertView 按钮转换视图的方式。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:@"Corp"]){
        DeckBuilderTableViewController *deckBuilder = [[DeckBuilderTableViewController alloc]init];
        [deckBuilder fetchCorpCards];
        [self performSegueWithIdentifier:@"deckBuilder" sender:self];
    }

    if ([title isEqualToString:@"Runner"]){
        DeckBuilderTableViewController *deckBuilder = [[DeckBuilderTableViewController alloc]init];
        [deckBuilder fetchRunnerCards];
        [self performSegueWithIdentifier:@"deckBuilder" sender:self];
    }
}

这里是 cellForRowAtIndexPath 方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath called");
    NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    if (self.corpCardsSelected == YES){
        CorpCard *corpCards = [self.datasource objectAtIndex:indexPath.row];
        cell.textLabel.text = corpCards.title;
    }

    if (self.runnerCardsSelected == YES){
        RunnerCard *runnerCards = [self.datasource objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@", runnerCards.title];
    }

    return cell;
}

编辑:

这里是委托方法

- (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.datasource.count;

}

我现在注意到的问题是数据源数组将记录其内容,但 numberOfRowsInSection 返回的计数为 0。

这是视图最初是如何启动和转换的。这段代码来自最初的 viewController。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:@"side1"]){
        DeckBuilderTableViewController *deckBuilder = [[DeckBuilderTableViewController alloc]init];
        [deckBuilder fetchSide1Cards];
        [self performSegueWithIdentifier:@"deckBuilder" sender:self];
    }

    if ([title isEqualToString:@"side2"]){
        DeckBuilderTableViewController *deckBuilder = [[DeckBuilderTableViewController alloc]init];
        [deckBuilder fetchSide2Cards];
        [self performSegueWithIdentifier:@"deckBuilder" sender:self];
    }
}

【问题讨论】:

  • 属性检查器中有什么? (顶部列表中的第四个图标)
  • 这不是归因。第一个字段显示“内容:动态原型”的那个
  • 我重新编辑了截图,这些是你要找的面板吗?
  • 好的。一切看起来都很好。你说 numberOfSections 和 numberOfRowsInSection 被调用,你返回 >0?
  • 有一个窗口,其中 numberOfRows = 0,但是我确实在 viewDidAppear 中调用 [self.tableView reloadData]

标签: ios objective-c uitableview


【解决方案1】:

如果cellForRowAtIndexPath 未被调用,那是因为delegate 设置不正确,您似乎拥有或部分和/或行数不正确。 确保您在这些方法中返回正确的值:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//should return at least 1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//should return at least 1

【讨论】:

  • 我已经添加了委托方法。我可以看到 numberOfRowsINSection 在某个时间点为 0,但我确实在 viewDidAppear method 中调用了 [self.tableView reloadData]
  • 我能想到的唯一另一件事是,由于某种原因,tableView 的框架以某种方式隐藏了行(太小或被覆盖)。除此之外,至少应该调用cellForRowAtIndexPath
【解决方案2】:

您的数据源和委托出口应设置为表视图控制器,而不是表视图。

【讨论】:

    【解决方案3】:

    由于您的数据源有一个计数,但另一个计数为 0,很明显您正在将其归零。检查您的视图[Will|Did]Appear 方法,以确保您没有第二次获取数据并且操作不正确。

    【讨论】:

    • viewDidAppear 和 viewDidAppear 方法没有任何代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多