Я создаю приложение (UITabBar), где храню NSMutableArray пользовательских объектов. Мой пользовательский объект называется DayModel.UITableViewController не обновляется правильно
Мой файл DayModel.h:
#import <Foundation/Foundation.h>
@interface DayModel : NSObject
@property (nonatomic, retain) NSDate *mydate;
@property (nonatomic) float myFloat;
@end
Мой DayModel.m файл:
#import "DayModel.h"
@implementation DayModel
@synthesize myDate, myFloat;
-(id)init {
// Init self
self = [super init];
if (self) {
// Setup
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:self.myDate forKey:@"myDate"];
[coder encodeObject:self.myFloat forKey:@"myFloat"];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [[DayModel alloc] init];
if (self != nil)
{
self.myDate = [coder decodeObjectForKey:@"myDate"];
self.myFloat = [coder decodeFloatForKey:@"myFloat"];
}
return self;
}
@end
"Основной" ViewController, экономя новые объекты:
// Add data to the DayModel class
DayModel *currentDay = [[DayModel alloc] init];
currentDay.myDate = myDate;
currentDay.myFloat = myFloat;
// Add currentDay to the _objects NSMutableArray
[_objects insertObject:currentDay atIndex:0];
// Save this array using NSKeyedArchiver
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_objects] forKey:@"objects"];
мой UITableViewController отображение:
viewWillAppear
// Load the _objects array
NSData *objectsData = [defaults objectForKey:@"objects"];
if (objectsData != nil)
{
NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:objectsData];
if (oldArray != nil)
{
_objects = [[NSMutableArray alloc] initWithArray:oldArray];
} else
{
_objects = [[NSMutableArray alloc] init];
}
} else
{
_objects = [[NSMutableArray alloc] init];
}
Другие методы:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [_objects count];
}
Загрузка данных:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Get the DayModel
DayModel *currentModel = [[DayModel alloc] init];
currentModel = _objects[indexPath.row];
// Get the UILabels
UILabel *dateLabel = (UILabel *)[cell viewWithTag:10];
UILabel *floatLabel = (UILabel *)[cell viewWithTag:20];
// Create the DateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
// Set the text
dateLabel.text = [dateFormatter stringFromDate:currentModel.myDate];
floatLabel.text = [NSString stringWithFormat:@"%.02f", currentModel.myFloat];
return cell;
}
воспроизводящих проблему:
- A dd item from tab nr 1
- Перейдите на вкладку № 2 (таблица). Данные отображаются правильно
- Перейдите на вкладку № 1 и добавьте новый объект
- Перейдите на вкладку № 2 (таблица). Новый элемент отображается с данными из предпросмотра, а не с новыми данными.
Когда приложение перезагружается, таблица отображается правильно.
EDIT
Что происходит, что новый элемент добавляется индекс-появляться в верхней части списка, в то время как класс TableView прибудет это новая информация из последней строки, когда он должен получать его от верхней. Как я могу «отменить» это?
Спасибо! Erik