ios - 在适当的部分动态添加行而不干扰其排序

标签 ios iphone objective-c cocoa-touch uitableview

我计划在适当的分隔符下添加假期(行)(月份作为部分)。到目前为止,我可以从 plist 中检索数据并创建具有预定义主题的部分(12 个月),但我无法找出在适当月份下添加假期的正确方法。

@synthesize event, sections;

- (void)viewDidLoad {

    self.event = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2013" ofType:@"plist"]];
    self.sections = [[NSMutableDictionary alloc] init];

    BOOL found;


    for (NSDictionary *oneEvent in self.event)
    {        
        NSString *c = [[oneEvent objectForKey:@"date"] substringToIndex:3];

        found = NO;

        for (NSString *str in [self.sections allKeys])
        {
            if ([str isEqualToString:c])
            {
                found = YES;
            }
        }

        if (!found)
        {     
            [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
    }


    for (NSDictionary *oneEvent in self.event)
    {
        [[self.sections objectForKey:[[oneEvent objectForKey:@"date"] substringToIndex:3]] addObject:oneEvent];
    }    


    [super viewDidLoad];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return [[self.sections allKeys] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *months = [[NSArray alloc]initWithObjects:@"January",@"February",@"March",@"April",@"May",@"June",@"July",@"August",@"September",@"October",@"November",@"December", nil];
    return [months objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [[self.sections valueForKey:[[self.sections allKeys]  objectAtIndex:section]] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    NSDictionary *results = [[self.sections valueForKey:[[self.sections allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    cell.textLabel.text = [results  objectForKey:@"date"];
    cell.detailTextLabel.text = [results objectForKey:@"event"];

    return cell;
}

当前结果:

result

最佳答案

在 cellForRowAtIndexPath 方法中,您假设 [self.sections allKeys] 与硬编码的“months”数组具有相同的顺序。解决此问题的一种方法是将“months”数组保留为属性,然后将该行更改为:

NSDictionary *results = [[self.sections valueForKey:[[self.months objectAtIndex:indexPath.section] substringToIndex:3]] objectAtIndex:indexPath.row];

可能更好的方法是将所有内容存储在数组中而不是字典中。我可能会使用 12 个字典的数组,每个字典都有“月份”和“假期”字段。像这样的事情:

self.sections = @[ @{@"month":@"January",@"holidays":@[…]}, @{@"month":@"February",@"holidays":@[…]},...]

关于ios - 在适当的部分动态添加行而不干扰其排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382518/

相关文章:

ios - 如何在 UITableViewDataSource 中重新加载数据

c# - Azure 媒体播放器无法在 iPhone 上使用 AES 保护

iphone - 强制 UIImagePickerController 以横向模式拍摄视频

iphone - 使用 MKOverlay 的 pointCounts 错误

ios - 无法点击 SKSpriteNode - 未检测到触摸 ios

ios - 从 Paypal MPL 到 PaypalMobile iOS SDK 的迁移问题

ios - 进度条实现

ios - 及时准确的连续数据算法

iphone - 仅对 subview 启用 UserInteraction

ios - 您可以将 Google 地球集成到您的 iOS 应用程序中吗?