为什么detailTextLabel不可见?

时间:2011-03-04 06:52:06

标签: objective-c uitableview cell detailtextlabel

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

6 个答案:

答案 0 :(得分:126)

detailTextLabel未显示cells样式UITableViewCellStyleDefaultinitUITableViewCell代替UITableViewCellStyleSubtitle,您应该会看到detailTextLabel

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

答案 1 :(得分:24)

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

Style cell property in Interface Builder.

答案 2 :(得分:1)

快捷键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
}

答案 3 :(得分:0)

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

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

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

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

答案 4 :(得分:0)

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

// 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!
}

答案 5 :(得分:0)

为了以编程方式解决它:

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