【问题标题】:detailtext label not showing up详细文本标签未显示
【发布时间】:2013-08-23 01:58:15
【问题描述】:

当我使用下面的sn-p时,详细文本标签不显示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"NEW";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath ];
    if(cell==nil)
    {     
    cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }
    NSDictionary* item = [saleItems objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForKey:@"name"];
    cell.detailTextLabel.text =  [item valueForKey:@"store"];

    return cell;




}

但是,当我将上述方法修改为以下详细信息时:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"NEW";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];


    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    NSDictionary* item = [saleItems objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForKey:@"name"];
    cell.detailTextLabel.text =  [item valueForKey:@"store"];

    return cell;


}

第一种方法出了什么问题? 使用 dequeueReusableCellWithIdentifier 的正确方法是什么?

【问题讨论】:

    标签: ios uitableview uikit


    【解决方案1】:

    根据这个SO post,注册UITableViewCell意味着所有的单元格都将被实例化为默认样式。 registerClass:forCellReuseIdentifier: 不提供字幕和左右详细信息单元格。

    【讨论】:

      【解决方案2】:

      因为您创建了默认样式。您问题中的某些方法可用于 iOS 6。您确定要针对 iOS 6

      您可以试试这个示例代码(不仅适用于 ios 6):

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString* cellIdentifier = @"NEW";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
          // if you sure the cell is not nil (created in storyboard or everywhere) you can remove "if (cell == nil) {...}"
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
          }
      
          NSDictionary* item = [saleItems objectAtIndex:[indexPath row]];
          cell.textLabel.text = [item valueForKey:@"name"];
          cell.detailTextLabel.text =  [item valueForKey:@"store"];
      
          return cell;
      

      }

      希望对您有所帮助!

      【讨论】:

        【解决方案3】:

        在第二种方法中,您不是将单元格出队,而是实际上创建了一个新单元格。这是不可取的。相反,使用第一种方法,但替换行:

        UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath ];
        

        UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        

        这是因为包含 indexPath 的方法总是会返回单元格,所以请检查;

        if(!cell)
        

        将始终返回 true,因此您将无法创建具有其他样式的单元格。但是使用没有索引路径的方法将返回 nil,如果之前没有创建单元格...您可以阅读更多关于 Apple 提供的 UITableViewCell 文档:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-03
          • 1970-01-01
          • 2023-03-09
          • 2019-09-23
          • 1970-01-01
          相关资源
          最近更新 更多