我正在尝试制作类似于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/

10-13 04:28