【问题标题】:UITableViewCell not Display Subtitle when i am set the initwithstyle:UItableviewsubtitle当我设置 initwithstyle:UItableviewsubtitle 时,UITableViewCell 不显示字幕
【发布时间】:2014-03-10 06:05:49
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellidenti = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellidenti];

    if(cell == Nil)
    {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidenti];
    }
    cell.textLabel.text = isActive?searchData[indexPath.row]:tableData[indexPath.row];
    cell.detailTextLabel.text = isActive?searchNumberData[indexPath.row]:tableNumberData[indexPath.row];   
    return cell;
}

我没有得到完美的视图,我没有使用故事板。

【问题讨论】:

  • 使用静态 NSString *cellidenti = @"Cell";
  • 你想用这个做什么:UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellidenti];

标签: ios iphone uitableview


【解决方案1】:

您没有使用正确的可重用单元格实例方式,并且还使用了静态单元格标识符。所以有关更多信息,请参阅编辑部分中的以下代码....

-UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellidenti = @"Cell";// edited here.....
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidenti];// edited here

    if(cell == Nil)
    {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidenti];
    }
    cell.textLabel.text = isActive?searchData[indexPath.row]:tableData[indexPath.row];
    cell.detailTextLabel.text = isActive?searchNumberData[indexPath.row]:tableNumberData[indexPath.row];   
    return cell;
}

【讨论】:

    【解决方案2】:

    我在代码中观察到的两件事

    1) 使用correct way of cell reusability 使用dequeueReusableCellWithIdentifier(正如其他人已经建议的那样)。还可以查看使用dequeueReusableCellWithIdentifier:forIndexPath: 的单元格here 的另一种可重用方式。

    2) 尝试使用nil 而不是Nil。看看this thread

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellidenti = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidenti];
    
        if(cell == nil)
        {
            cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidenti];
        }
        cell.textLabel.text = isActive?searchData[indexPath.row]:tableData[indexPath.row];
        cell.detailTextLabel.text = isActive?searchNumberData[indexPath.row]:tableNumberData[indexPath.row];   
        return cell;
    }
    

    【讨论】:

      【解决方案3】:

      错误在这一行UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellidenti]; 这将返回UIView,而不是UITableviewCell。相反,您可以使用以下行。

      [tableView dequeueReusableCellWithIdentifier:cellidenti];
      

      【讨论】:

        【解决方案4】:

        使用dequeueReusableCellWithIdentifier 创建重用实例

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-27
          • 2017-05-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多