【问题标题】:Why is detailTextLabel not visible?为什么 detailTextLabel 不可见?
【发布时间】:2011-07-08 14:51:53
【问题描述】:

detailTextLabel 不可见(代码如下)。你能告诉我为什么吗?

 // Customize the appearance of table view cells.
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...

NSString *cellValue = [myListArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

cell.detailTextLabel.text = @"Hello "; // This is not visible
cell.image = [myListArrayImages objectAtIndex:indexPath.row];

return cell;
}

【问题讨论】:

    标签: objective-c uitableview cell detailtextlabel


    【解决方案1】:

    detailTextLabel 不会为具有 UITableViewCellStyleDefault 样式的 cells 显示。 init UITableViewCellUITableViewCellStyleSubtitle 代替,你应该看到你的 detailTextLabel

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    

    【讨论】:

      【解决方案2】:

      或者,如果您使用的是 Interface Builder,请将 Style 单元格属性更改为 Subtitle。 :)

      【讨论】:

      • 如果以编程方式执行,该方法的等价物是什么?
      • 谢谢大家,我正在努力
      【解决方案3】:

      斯威夫特 5

      您可以在 cellForRowAt 方法中启用此功能

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
              cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
      
          cell.textLabel?.text = qus[indexPath.row]
          cell.detailTextLabel?.text = ans[indexPath.row]
          return cell
      }
      

      【讨论】:

      • 可能是对您所做的不同之处的一些介绍,可能对其他人有所帮助。
      • 我很困惑,为什么要在下一行重新声明单元格时使用dequeueReusableCell?
      【解决方案4】:

      为了以编程方式解决它:

      let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "identifier")
      

      【讨论】:

        【解决方案5】:

        我用过这个,它对我有用:

        // programming mark ----- ----- ---- ----- ----- ---- ----- ----- ----
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let CellIdentifier: String = "CellIdentifier"
        
            var cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as? UITableViewCell
        
            if cell == nil {
                cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier)
            }
        
            cell!.textLabel!.text = "Title"
            cell!.detailTextLabel!.text = "Value"
        
            return cell!
        }
        

        【讨论】:

          【解决方案6】:

          我只想提一下,UITableViewCell 类参考中的措辞在这个问题上可能有点混乱:

          (在描述了每种细胞类型之后)

          "讨论 在所有这些单元格样式中,较大的文本标签通过 textLabel 属性访问,较小的文本标签通过 detailTextLabel 属性访问。”

          似乎是说所有单元格类型都包含detailTextLabel,但如果您仔细阅读它们,只有默认类型没有有detailTextLabel .

          【讨论】:

            猜你喜欢
            • 2016-08-13
            • 1970-01-01
            • 1970-01-01
            • 2015-10-02
            • 2015-06-14
            • 2011-09-29
            • 2018-09-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多