2014-12-07 2 views
0

사용자 지정 개체의 NSMutableArray를 저장하고있는 앱 (UITabBar)을 만들고 있습니다. 내 사용자 지정 개체는 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]; 
} 

다른 방법 :

  • A : 문제의 재생

    - (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; 
    } 
    

    :

    - (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]; 
    } 
    

    데이터로드 dd 항목의 탭 번호 1

  • nr 2 (테이블)로 이동하십시오. 데이터가 올바르게 표시됩니다.
  • nr 1 탭으로 이동하여 새 개체를 추가하십시오.
  • nr 2 (표)로 이동하십시오. 새 항목은 미리보기 항목의 데이터와 함께 표시되며 새 데이터는 표시되지 않습니다.

앱을 다시로드하면 표가 올바르게 표시됩니다.

편집

무엇 발생하면있는 tableview 클래스의 get은 그것을 점점되어야한다 마지막 행에서 새 정보가있는 동안 새 항목이 목록의 맨 위에 표시하는 인덱스 0에 추가됩니다이다 위로부터. 어떻게 이것을 "되돌릴"수 있습니까?

감사합니다. Erik

답변

0

새 항목을 추가하는 것뿐만 아니라 전체 목록을 다시로드하여 문제를 해결했습니다. 이 코드는 NSUserDefaults (viewWillAppear)에서 목록을로드하는 행 아래에 있습니다. 더 나은 솔루션이 있는지

// Reload the UITableView completely 
[self.tableView reloadData]; 

알려주세요 :)

관련 문제