ios - 将 TableView 数组拆分为标签和副标题

标签 ios cocoa-touch uikit

刚接触 Xcode,我正在浏览一些苹果文档,它展示了如何设置文本和副标题。我对这些指南的问题是如何实现它们。

这就是我的数组当前的样子:

- (void)viewDidLoad {
    [super viewDidLoad];

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    //Add items
    [listOfItems addObject:@"Iceland"];
    [listOfItems addObject:@"Greenland"];
    [listOfItems addObject:@"Switzerland"];
    [listOfItems addObject:@"Norway"];
    [listOfItems addObject:@"New Zealand"];
    [listOfItems addObject:@"Greece"];
    [listOfItems addObject:@"Rome"];
    [listOfItems addObject:@"Ireland"];

}

这就是它返回数据的方式:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    return cell;
}

这只是标题,我想添加一个副标题。 我需要插入什么代码来实现字幕?

提前致谢

最佳答案

要设置单元格的副标题,请使用 detailtextLabel 属性:

cell.detailTextLabel.text = @"My Subtitle";

如果您希望副标题以正常方式显示(左对齐,位于主文本标签下方,并采用稍小的灰色字体),您还应该确保单元格的样式设置为 UITableViewCellStyleSubtitle/p>

关于ios - 将 TableView 数组拆分为标签和副标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942366/

相关文章:

ios - 通过 iOS 应用程序发送 Facebook 消息(在后台)

ios - Xcode 在您的钥匙串(keychain)中找不到此配置文件的有效私钥证书对

ios - 是否有将实例变量与 NSLayoutConstraint 的可视格式一起使用的最佳实践?

objective-c - 新的自动引用计数机制是如何工作的?

ios - 如何让 DTCoreText 使用系统字体?

ios - 如何同时翻转和旋转标签?

ios - Stripe 创建客户 iOS

ios - 从外部 url 打开已安装的 PWA

iphone - 从 plist 中提取 NSDates 时出错

iphone - 用于推送 UIViewController 的自定义动画