Ios 如何在NSArray中添加字幕
我正在寻找一种更好的添加字幕的方法,目前有两种不同的数组,我想知道你是否可以将字幕添加到与标题相同的数组中Ios 如何在NSArray中添加字幕,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在寻找一种更好的添加字幕的方法,目前有两种不同的数组,我想知道你是否可以将字幕添加到与标题相同的数组中 lodgeList = [[NSArray alloc]initWithObjects: //Abingdon @"Abingdon Lodge No. 48", // This is the Title // I would like to add the subtitle here
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;
}
您还可以使用NSDictionary,而不是自定义类
lodgeList = @[
@{@"title":@"Abingdon Lodge No. 48",
@"subtitle": @"a dream of a lodge"},
@{@"title":@"A. Douglas Smith, Jr. No. 194",
@"subtitle": @"Smith's logde…"},
];
此代码的特点是
实际上,您可以对
tableView:cellforrowatinexpath:
使用相同的实现,因为这两种解决方案都有:
-(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;
}
这能与原型细胞一起工作吗
是的,如中所示,如果要创建UITableViewCell的子类,请为其指定一个属性以保存模型,并为其指定属性以反映标签。这些标签将连接到故事板中,创建一个类,该类具有标题和副标题的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;
}
您还可以使用NSDictionary,而不是自定义类
lodgeList = @[
@{@"title":@"Abingdon Lodge No. 48",
@"subtitle": @"a dream of a lodge"},
@{@"title":@"A. Douglas Smith, Jr. No. 194",
@"subtitle": @"Smith's logde…"},
];
此代码的特点是
实际上,您可以对
tableView:cellforrowatinexpath:
使用相同的实现,因为这两种解决方案都有:
-(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;
}
这能与原型细胞一起工作吗 是的,如中所示,如果要创建UITableViewCell的子类,请为其指定一个属性以保存模型,并为其指定属性以反映标签。这些标签将连接到故事板中