gpt4 book ai didi

ios - NSDictionary 和 UITableView 副标题

转载 作者:行者123 更新时间:2023-11-29 02:01:12 30 4
gpt4 key购买 nike

我正在尝试在 Objective-C 中制作电话簿之类的应用程序。我使用 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 中的每个对象都是一个字典数组,每个字典都有 titlesubtitle

然后:

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

关于ios - NSDictionary 和 UITableView 副标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30367535/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com