如何为NSArray添加字幕

App*_*pps 1 objective-c uitableview ios

我试图找到一个更好的方法来添加字幕目前有两个不同的数组我想知道你是否可以在与标题相同的数组中添加字幕?

lodgeList = [[NSArray alloc]initWithObjects:

             //Abingdon
             @"Abingdon Lodge No. 48",  // This is the Title 
             // I would like to add the subtitle here

             @"York Lodge No. 12",

             //Alberene
             @"Alberene Lodge No. 277",

             // Alexandria
             @"A. Douglas Smith, Jr. No. 1949",
             @"Alexandria-Washington Lodge No. 22",
             @"Andrew Jackson Lodge No. 120",
             @"Henry Knox Field Lodge No. 349",
             @"John Blair Lodge No. 187",
             @"Mount Vernon Lodge No. 219",
Run Code Online (Sandbox Code Playgroud)

如何在上面的每个名称中添加字幕?

Vik*_*ica 6

创建一个类,它具有标题和副标题的NSString属性.实例化对象.投入阵列.

-(UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //…
    MyLodge *lodge = lodgeList[indexPath.row];
    cell.textLabel.text = lodge.title;
    cell.detailLabel.text = lodge.subtitle;
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

您也可以使用NSDictionaries而不是自定义类.

lodgeList = @[ 
                @{@"title":@"Abingdon Lodge No. 48",
                  @"subtitle": @"a dream of a lodge"},
                @{@"title":@"A. Douglas Smith, Jr. No. 194",
                  @"subtitle": @"Smith's logde…"},
             ];
Run Code Online (Sandbox Code Playgroud)

此代码具有新的文字语法

-(UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //…
    NSDictionary *lodge = lodgeList[indexPath.row];
    cell.textLabel.text = [lodge objectForKey:@"title"];
    cell.detailLabel.text = [lodge objectForKey:@"subtitle"];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

实际上,tableView:cellForRowAtIndexPath:由于两种解决方案的键值编码,您可以使用相同的实现:

-(UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //…
    id lodge = lodgeList[indexPath.row];
    cell.textLabel.text = [lodge valueForKey:@"title"];
    cell.detailLabel.text = [lodge valueForKey:@"subtitle"];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

这适用于原型细胞吗?

是的,如"WWDC 2011,Session 309 - Interface Builder Storyboarding介绍"中所示,您将创建UITableViewCell的子类,为其提供一个属性来保存您的模型和属性以反映标签.这些标签将在故事板中连接起来