重新加载UITableView数据(在字典中查找indexPathForRow)



我是一个练习Objective-C开发iOS应用程序的新手。"handleRefresh"中的最后2行似乎不起作用,因为我找不到新日期的indexPathForRow并将其传递给UITableView,也无法将新日期放入dictionaryOfNumbers中。你能帮我吗?

//
//  ViewController.m
//  deleteTableView
//
//  Created by Mehmetcan Oralalp on 25/11/13.
//  Copyright (c) 2013 Mehmetcan Oralalp. All rights reserved.
//
#import "ViewController.h"
static NSString *CellIdentifier = @"NumbersCellIdentifier";
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableViewNumbers;
@property (nonatomic, strong) NSMutableDictionary *dictionaryOfNumbers;
@property (nonatomic, strong) UIBarButtonItem *barButtonAction;
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@property (nonatomic, strong) NSMutableArray *allTimes;
@end
static NSString *SectionOddNumbers = @"Odd Numbers";
static NSString *SectionEvenNumbers = @"Even Numbers";
static NSString *SectionDynamicDate = @"Dynamic Date";
@implementation ViewController
//use refresh
- (void) handleRefresh:(id)paramSender{
    /* Put a bit of delay between when the refresh control is released
      and when we actually do the refreshing to make the UI look a bit
      smoother than just doing the update without the animation */
    int64_t delayInSeconds = 1.0f;
    dispatch_time_t popTime =
    dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        /* Add the current date to the list of dates that we have
         so that when the table view is refreshed, a new item will appear
         on the screen so that the user will see the difference between
         the before and the after of the refresh */
        [self.allTimes addObject:[NSDate date]];
        //PROBLEM IS HERE!!!
        NSIndexPath *indexPathOfNewRow =
        [NSIndexPath indexPathForRow:0 inSection:2];
        [self.tableViewNumbers insertRowsAtIndexPaths:@[indexPathOfNewRow] withRowAnimation:UITableViewRowAnimationAutomatic];
    });
}
//refresh ends here

- (UITableViewCell *) tableView:(UITableView *)tableView
          cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                           forIndexPath:indexPath];
    NSString *sectionNameInDictionary =
    self.dictionaryOfNumbers.allKeys[indexPath.section];
    NSArray *sectionArray = self.dictionaryOfNumbers[sectionNameInDictionary];
    NSNumber *number = sectionArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",
                           number];
    return cell;
}
- (NSMutableDictionary *) dictionaryOfNumbers{
    if (_dictionaryOfNumbers == nil){
        NSMutableArray *arrayOfEvenNumbers =
        [[NSMutableArray alloc] initWithArray:@[
                                                @0,
                                                @2,
                                                @4,
                                                @6,
                                                ]];
        NSMutableArray *arrayOfOddNumbers =
        [[NSMutableArray alloc] initWithArray:@[
                                                @1,
                                                @3,
                                                @5,
                                                @7,
                                                ]];
        self.allTimes = [NSMutableArray arrayWithObject:[NSDate date]];
        NSMutableArray *arrayOfDate = [[NSMutableArray alloc] initWithArray:self.allTimes];
        _dictionaryOfNumbers =
        [[NSMutableDictionary alloc]
         initWithDictionary:@{
                              SectionDynamicDate : arrayOfDate,
                              SectionEvenNumbers : arrayOfEvenNumbers,
                              SectionOddNumbers : arrayOfOddNumbers,
                              }];
    }
    return _dictionaryOfNumbers;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.barButtonAction =
    [[UIBarButtonItem alloc]
     initWithTitle:@"Delete Odd Numbers"
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(deleteOddNumbersSection:)];
    [self.navigationItem setRightBarButtonItem:self.barButtonAction
                                      animated:NO];
    self.tableViewNumbers = [[UITableView alloc]
                             initWithFrame:self.view.frame
                             style:UITableViewStyleGrouped];
    [self.tableViewNumbers registerClass:[UITableViewCell class]
                  forCellReuseIdentifier:CellIdentifier];
    self.tableViewNumbers.autoresizingMask =
    UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleHeight;
    self.tableViewNumbers.delegate = self;
    self.tableViewNumbers.dataSource = self;
    [self.view addSubview:self.tableViewNumbers];
    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl = self.refreshControl;
    [self.refreshControl addTarget:self
                            action:@selector(handleRefresh:)
                  forControlEvents:UIControlEventValueChanged];
    [self.tableViewNumbers addSubview:self.refreshControl];
}
@end

代码的问题是使用字典作为表视图。字典中的键/值对没有定义的顺序。

您应该使用数组,它最初可能看起来像:

NSArray *dataSource = @[
                        @{@"title": @"Dynamic Date",
                          @"rows" : [ @[[NSDate date]] mutableCopy] },
                        @{@"title": @"Even Numbers",
                          @"rows" : [ @[@0, @2, @4] mutableCopy] },
                        @{@"title": @"Odd Numbers",
                          @"rows" : [ @[@1, @3, @5] mutableCopy] },
                        ];

这大大简化了所有数据源方法。例如,

NSString *title = dataSource[section][@"title"];

是一个部分的标题,

NSString *item = dataSource[indexPath.section][@"rows"][indexPath.row];

是给定索引路径中的项。

如果你插入一个新项目(例如另一个日期)在其中一个"行"数组中,您可以确切地知道它将出现在表视图。例如,在第0节中添加另一个日期:

NSMutableArray *dates = dataSource[0][@"rows"];
NSIndexPath *indexPathOfNewRow = [NSIndexPath indexPathForRow:[dates count] inSection:0];
[dates addObject:[NSDate date]];
[self.tableViewNumbers insertRowsAtIndexPaths:@[indexPathOfNewRow] withRowAnimation:UITableViewRowAnimationAutomatic];

最新更新