我是一名新手练习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
答案 0 :(得分:0)
您的代码存在的问题是您使用字典作为数据源 表视图。字典中的键/值对没有定义的顺序。
您应该使用数组,最初看起来像:
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];