【问题标题】:NSDictionary and UITableView subtitleNSDictionary 和 UITableView 副标题
【发布时间】:2015-05-21 07:43:14
【问题描述】:

我正在尝试在 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;
}

如果能提供任何帮助,我将不胜感激!

【问题讨论】:

    标签: ios objective-c uitableview nsdictionary subtitle


    【解决方案1】:

    在您的情况下,有 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;
    }
    

    【讨论】:

    • 我认为这可能是正确的解决方案。我还对你投了赞成票,这样你就更接近被允许发表评论了(因为通常当你要求某人尝试某事时,你会在评论中这样做)。
    • 是的,低于 50 的声望让我无法发表评论。
    • 我知道那种感觉:)
    • 你是对的,在示例代码中我只有 1 行的 1 部分。更改线路有效,但是如果我在一个部分中有多行,则整个部分仍然存在相同的重复问题。问题变成了如果我在一个部分中有多行该怎么办?
    【解决方案2】:

    这不起作用,因为您有从 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 2012-12-27
      相关资源
      最近更新 更多