TableView的cell的detailTextLabel不显示内容的问题

问题

对于初学者来说,写tableView的时候有没有遇到过在这样的问题:TableView的cell的detailTextLabel不显示的问题。

关于这两个属性

detailTextLabel系统的TableView有这个两个属性,一个textLabel,一个detailTextLabel,如下官方文档:

// default is nil.  label will be created if necessary.
@property (nonatomic, readonly, strong, nullable) UILabel *textLabel NS_AVAILABLE_IOS(3_0); 

// default is nil.  label will be created if necessary (and the current style supports a detail label).
@property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel NS_AVAILABLE_IOS(3_0); 

问题总结

文档上说default is nil,意思是使用默认的cell的时候,这两个label是空的?还是说这个cell在不用的时候是nil,用的时候会自己创建,这个比较模棱两可。

如果你要使用系统的cell实现textLabel和detailTextLabel显示的效果的时候,创建cell的时候将cell的类型指定为UITableViewCellStyleDefault,这时候你会发现,textLabel能显示出来,而detailTextLabel是显示不出来的。所以说default is nil?

所以如果要实现这种效果你需要将cell的类型指定为UITableViewCellStyleSubtitle类型。

// Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
UITableViewCellStyleSubtitle    

使用UITableViewCellStyleSubtitle类型的cell就能显示出来detailTextLabel的内容了,但是Used in iPod是什么鬼?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"AddressSelectedController_cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    
    DDSearchPoi *poi = self.dataSource[indexPath.row];

    cell.textLabel.text = poi.name;
    cell.detailTextLabel.text = poi.address;

    return cell;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,962评论 3 38
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,378评论 30 472
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 10,236评论 1 9
  • TableView的重用机制,为了做到显示和数据分离,IOS tableView的实现并且不是为每个数据项创建一个...
    陌尚煙雨遙阅读 10,875评论 4 6
  • 一、简介 官方给出了比较全面的介绍,要点摘录如下: table view的作用:导航、展示索引列表、展示详情信息、...
    quantiza阅读 4,095评论 0 1