当我设置initwithstyle:uitaiteViewSubtitle时,uitaiteViewCell不显示字幕


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

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

您不使用可重复使用的单元格的实例的正确方法,也没有使用静态单元格标识符。因此,有关更多信息,请参见编辑部分中的代码。

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

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

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

2)尝试使用nil而不是Nil。看看这个线程。

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

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

[tableView dequeueReusableCellWithIdentifier:cellidenti];

使用dequeueReusableCellWithIdentifier创建重复使用实例

最新更新