简体   繁体   English

字幕没有出现UITableView单元格

[英]Subtitle not appearing UITableView cell

I am new to app designing and Objective-C. 我是应用程序设计和Objective-C的新手。 I am writing a simple food app that has some food stored in an array with details of the food, food name, cook time and the file name which leads to a picture. 我正在写一个简单的食物应用程序,其中有一些食物存储在一个数组中,其中包含食物,食物名称,烹饪时间和文件名的详细信息。

I am trying to get a subtitle to work but it isn't showing up. 我想要一个副标题工作,但它没有出现。 I've read various other posts and have tried to implement some of the answers but none have worked. 我已经阅读了其他各种帖子,并试图实现一些答案,但没有一个有效。

This is my code if there is anything obviously incorrect please let me know. 这是我的代码,如果有任何明显不正确的请告诉我。 Thank you. 谢谢。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"FoodCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    Food *current = [foodArray objectAtIndex:indexPath.row];


    [cell.textLabel setText:[current name]];
    [[cell detailTextLabel] setText:@[current cookTime]];
    cell.imageView.image = [UIImage imageNamed:[current fileName]];

    return cell;
}

Replace 更换

[[cell detailTextLabel] setText:@[current cookTime]];

with

[[cell detailTextLabel] setText:[current cookTime]];

Note that this assumes cookTime is an NSString . 请注意,这假设cookTimeNSString If it is something else, like an integer representing minutes, you would do something like this: 如果它是其他东西,比如代表分钟的整数,你会做这样的事情:

[[cell detailTextLabel] setText:[NSString stringWithFormat:@"%d minutes", [current cookTime]]];

Additionally, you may find it easier to use this format: 此外,您可能会发现使用此格式更容易:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%d minutes", [current cookTime]];

Try to replace: 尝试替换:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

with: 有:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

试着用

cell.detailTextLabel.text = [current cookTime];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM