NSDictionary 和 UITableView 副标题
NSDictionary and UITableView subtitle
我正尝试在 Objective-C 中制作一本 phone 类似书籍的应用程序。我使用 NSDictionary 来填充我的 UITableViewCell
。我得到了正确的标题、索引和章节,但是我无法为每个标题获得正确的副标题。每个标题的副标题始终相同。这是我的代码示例:
@interface TableViewController (){
NSDictionary *sarkilar;
NSArray *sarkilarSectionTitles;
NSArray *sarkilarIndexTitles;
NSArray *subtitles;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
sarkilar = @{@"A" : @[@"Alayına İsyan"],
@"B" : @[@"Bebek"],
@"E" : @[@"Elbet Birgün Buluşacağız"],
@"Y" : @[@"Yusuf Yusuf"]};
subtitles = [[NSArray alloc]initWithObjects:@"Seslendiren: Mustafa Sandal",
@"Seslendiren: Akın",
@"Seslendiren: Ahmet Özhan",
@"Seslendiren: Acil Servis",nil];
sarkilarSectionTitles = [[sarkilar allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
sarkilarIndexTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
.
.
.
//and in the cell set up i got this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
NSString *sarki = [sectionSarkilar objectAtIndex:indexPath.row];
cell.textLabel.text = sarki;
cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.row];
return cell;
}
如有任何帮助,我将不胜感激!
在您的案例中,有 4 个部分:"A"、"B"、"E" 和 "Y"。
每个部分只有 1 行。
所以,我假设你的 cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.row];
不断获得 "Seslendiren: Mustafa Sandal" 值?
尝试将行更改为 cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.section];
以查看问题是否已解决。
编辑:
我会将 sarkilar
字典更改为以下模式:
sarkilar=@{
@"A": @[@{
@"title": @"Alayına İsyan",
@"subtitle": @"Seslendiren: Mustafa Sandal"
},
@{
@"title": @"Title 2",
@"subtitle": @"Subtitle 2"
}],
@"B":.....
.
.
.
.
};
所以sarkilar
里面的每个对象都是一个字典数组,每个字典有title
和subtitle
。
然后:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
NSDictionary *dict = [sectionSarkilar objectAtIndex:indexPath.row];
NSString *title = [dict objectForKey:@"title"];
NSString *subtitle = [dict objectForKey:@"subtitle"];
cell.textLabel.text = title;
cell.detailTextLabel.text = subtitle;
}
这将不起作用,因为您拥有从 A 到 Z 的密钥,并且您正在通过密钥(A 到 Z)获取 sarkilar 值。
并且您正在通过 indexPath.row
设置 字幕
如果你改变你的结构并从单一数据源获取数据会更好。
喜欢,
NSDictionary *mainSource;
mainSource = @{@"A": @[@{@"title": @"Alayına İsyan", @"subTitle":@"Seslendiren: Mustafa Sandal"}],
@"B": @[@{@"title": @"Bebek", @"subTitle":@"Seslendiren: Akın"}],
@"E": @[@{@"title": @"Elbet Birgün Buluşacağız", @"subTitle":@"Seslendiren: Ahmet Özhan"}],
@"Y": @[@{@"title": @"Yusuf Yusuf", @"subTitle":@"Seslendiren: Acil Servis"}]
};
并在 cellForRowAtIndexPath 中更改从源获取数据的方式。
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *currentContact = [mainSource objectForKey:sectionTitle];
NSString *strTitle = [[currentContact objectAtIndex:indexPath.row] objectForKey:@"title"]];
NSString *strSubtitle = [[currentContact objectAtIndex:indexPath.row] objectForKey:@"subTitle"]];
cell.textLabel.text = strTitle;
cell.detailTextLabel.text = strSubtitle;
return cell;
我正尝试在 Objective-C 中制作一本 phone 类似书籍的应用程序。我使用 NSDictionary 来填充我的 UITableViewCell
。我得到了正确的标题、索引和章节,但是我无法为每个标题获得正确的副标题。每个标题的副标题始终相同。这是我的代码示例:
@interface TableViewController (){
NSDictionary *sarkilar;
NSArray *sarkilarSectionTitles;
NSArray *sarkilarIndexTitles;
NSArray *subtitles;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
sarkilar = @{@"A" : @[@"Alayına İsyan"],
@"B" : @[@"Bebek"],
@"E" : @[@"Elbet Birgün Buluşacağız"],
@"Y" : @[@"Yusuf Yusuf"]};
subtitles = [[NSArray alloc]initWithObjects:@"Seslendiren: Mustafa Sandal",
@"Seslendiren: Akın",
@"Seslendiren: Ahmet Özhan",
@"Seslendiren: Acil Servis",nil];
sarkilarSectionTitles = [[sarkilar allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
sarkilarIndexTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
.
.
.
//and in the cell set up i got this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
NSString *sarki = [sectionSarkilar objectAtIndex:indexPath.row];
cell.textLabel.text = sarki;
cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.row];
return cell;
}
如有任何帮助,我将不胜感激!
在您的案例中,有 4 个部分:"A"、"B"、"E" 和 "Y"。
每个部分只有 1 行。
所以,我假设你的 cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.row];
不断获得 "Seslendiren: Mustafa Sandal" 值?
尝试将行更改为 cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.section];
以查看问题是否已解决。
编辑:
我会将 sarkilar
字典更改为以下模式:
sarkilar=@{
@"A": @[@{
@"title": @"Alayına İsyan",
@"subtitle": @"Seslendiren: Mustafa Sandal"
},
@{
@"title": @"Title 2",
@"subtitle": @"Subtitle 2"
}],
@"B":.....
.
.
.
.
};
所以sarkilar
里面的每个对象都是一个字典数组,每个字典有title
和subtitle
。
然后:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
NSDictionary *dict = [sectionSarkilar objectAtIndex:indexPath.row];
NSString *title = [dict objectForKey:@"title"];
NSString *subtitle = [dict objectForKey:@"subtitle"];
cell.textLabel.text = title;
cell.detailTextLabel.text = subtitle;
}
这将不起作用,因为您拥有从 A 到 Z 的密钥,并且您正在通过密钥(A 到 Z)获取 sarkilar 值。
并且您正在通过 indexPath.row
设置 字幕如果你改变你的结构并从单一数据源获取数据会更好。
喜欢,
NSDictionary *mainSource;
mainSource = @{@"A": @[@{@"title": @"Alayına İsyan", @"subTitle":@"Seslendiren: Mustafa Sandal"}],
@"B": @[@{@"title": @"Bebek", @"subTitle":@"Seslendiren: Akın"}],
@"E": @[@{@"title": @"Elbet Birgün Buluşacağız", @"subTitle":@"Seslendiren: Ahmet Özhan"}],
@"Y": @[@{@"title": @"Yusuf Yusuf", @"subTitle":@"Seslendiren: Acil Servis"}]
};
并在 cellForRowAtIndexPath 中更改从源获取数据的方式。
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *currentContact = [mainSource objectForKey:sectionTitle];
NSString *strTitle = [[currentContact objectAtIndex:indexPath.row] objectForKey:@"title"]];
NSString *strSubtitle = [[currentContact objectAtIndex:indexPath.row] objectForKey:@"subTitle"]];
cell.textLabel.text = strTitle;
cell.detailTextLabel.text = strSubtitle;
return cell;