59

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

6 回答 6

129

detailTextLabelcellsUITableViewCellStyleDefault样式 一起显示。代替,你应该看到你init的.UITableViewCellUITableViewCellStyleSubtitledetailTextLabel

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
于 2011-03-11T01:27:05.297 回答
31

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

Interface Builder 中的样式单元格属性。

于 2016-07-24T00:43:24.490 回答
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
}
于 2020-05-06T11:34:26.390 回答
2

为了以编程方式解决它:

let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "identifier")
于 2019-07-25T09:54:57.220 回答
1

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

// 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!
}
于 2015-03-21T06:17:39.073 回答
0

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

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

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

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

于 2013-10-31T20:06:18.723 回答