1

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

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",

如何为上面的每个名称添加副标题?

4

1 回答 1

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

除了自定义类,您还可以使用 NSDictionaries。

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

此代码具有新的文字语法

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

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

这可以与原型细胞一起使用吗?

是的,如«WWDC 2011, Session 309 — Introducing Interface Builder Storyboarding»中所示,您将创建 UITableViewCell 的子类,给它一个属性来保存您的模型和属性以反映标签。这些标签将连接在故事板中

于 2012-12-16T17:14:26.260 回答