我正在尝试找到一种更好的添加字幕的方法,目前有两个不同的数组,我想知道您是否可以将字幕添加到与标题相同的数组中?
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",
我将如何为上面的每个名称添加副标题?
创建一个类,该类具有标题和副标题的 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,会话309 - 介绍界面生成器情节提要»所示,您将创建UITableViewCell的子类,为其提供属性以保存模型和属性以反映标签。这些标签将在故事板中连接起来