2014-04-04 19 views
0

好吧我有三個UITableViews,我需要都來自相同的viewcontroller代碼。我需要從.m文件AG_ViewController中填充它們。來自一個來源的多個視圖和UITables,如何填充它們?

AG_ViewController.h

#import <UIKit/UIKit.h> 

    @interface AG_ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{ 

     IBOutlet UITableView *yesterdayTableView; 
     IBOutlet UITableView *tomorrowTableView; 
     IBOutlet UITableView *tableView; 
     IBOutlet UILabel *todaysDate; 
     IBOutlet UILabel *subtractDate; 
     IBOutlet UILabel *addDate; 
    } 
    @end 

AG_ViewController.m

#import "AG_ViewController.h" 
#import "AG_Storage.h" 
#import "AG_AddItemViewController.h" 

@interface AG_ViewController() 

@property NSMutableArray *mainArray; 
@property NSMutableArray *yesterdayArray; 
@property NSMutableArray *tomorrowArray; 
@property NSDate *todayDate; 
@property NSDate *tomorrowsDate; 
@property NSDate *yesterdaysDate; 
@property int counter; 

@property(weak,nonatomic)IBOutlet UIButton *yesterdayButton; 
@property(weak,nonatomic)IBOutlet UIButton *tomorrowButton; 
@property(weak,nonatomic)IBOutlet UIButton *backButton; 

@end 

@implementation AG_ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.mainArray = [[NSMutableArray alloc] init]; 
    self.yesterdayArray = [[NSMutableArray alloc]init]; 
    self.tomorrowArray = [[NSMutableArray alloc]init]; 
    [self loadInitialData]; 
} 

- (void)loadInitialData 
{ 
    // Do any additional setup after loading the view, typically from a nib. 


    //NSDate Info 

    NSTimeInterval secondsPerDay = 24 * 60 * 60; 
    NSDate *today = [[NSDate alloc]init]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"MMMM dd, yyyy"]; 

    self.todayDate = today; 
    self.tomorrowsDate = [today dateByAddingTimeInterval: secondsPerDay]; 
    self.yesterdaysDate = [today dateByAddingTimeInterval: -secondsPerDay]; 

    NSString *todayString = [dateFormat stringFromDate:self.todayDate]; 
    NSString *tomorrowString = [dateFormat stringFromDate:self.tomorrowsDate]; 
    NSString *yesterdayString = [dateFormat stringFromDate:self.yesterdaysDate]; 

    todaysDate.text = todayString; 
    addDate.text = tomorrowString; 
    subtractDate.text = yesterdayString; 

    AG_Storage *theDateToday = [[AG_Storage alloc]init]; 
    theDateToday.todaysDate = self.todayDate; 



    //Populate mainArray 
    for (int x= 0; x!=-99; x++) { 
     //get ready to call the NSUserDefaults 


     NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%d",todayString,x]]; 
     AG_Storage *someStorageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
     AG_Storage *storeToArray = [[AG_Storage alloc]init]; 
     storeToArray.itemName = someStorageObject; 


     if (someStorageObject != nil) { 
      //add the data to the mainArray and update counter 
      [self.mainArray addObject:storeToArray]; 
      self.counter++; 
     } 
     else if ((someStorageObject == nil) && (x==99)){ 
      //exit when done 
      x=-100; 


     } 

    } 


    //Populate yesterdayArray 
    for (int x= 0; x!=-99; x++) { 
     //get ready to call the NSUserDefaults 


     NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%d",yesterdayString,x]]; 
     AG_Storage *someStorageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
     AG_Storage *storeToArray = [[AG_Storage alloc]init]; 
     storeToArray.itemName = someStorageObject; 


     if (someStorageObject != nil) { 
      //add the data to the mainArray and update counter 
      [self.yesterdayArray addObject:storeToArray]; 
      self.counter++; 
     } 
     else if ((someStorageObject == nil) && (x==99)){ 
      //exit when done 
      x=-100; 


     } 

    } 


    //Populate tomorrowArray 
    for (int x= 0; x!=-99; x++) { 
     //get ready to call the NSUserDefaults 


     NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%d",tomorrowString,x]]; 
     AG_Storage *someStorageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
     AG_Storage *storeToArray = [[AG_Storage alloc]init]; 
     storeToArray.itemName = someStorageObject; 


     if (someStorageObject != nil) { 
      //add the data to the mainArray and update counter 
      [self.tomorrowArray addObject:storeToArray]; 
      self.counter++; 
     } 
     else if ((someStorageObject == nil) && (x==99)){ 
      //exit when done 
      x=-100; 


     } 

    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    if(tableView == tomorrowTableView){ 
     return [self.tomorrowArray count]; 
    } 
    else if(tableView == yesterdayTableView) 
     return [self.yesterdayArray count]; 
    else 
     return [self.mainArray count]; 
} 


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

    UITableViewCell *cell = [tableViewer dequeueReusableCellWithIdentifier:@"thisCell"]; 
    AG_Storage *toDoItem = [self.mainArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = toDoItem.itemName; 



    if (toDoItem.completed) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else{ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 

} 



- (IBAction)unwindToList:(UIStoryboardSegue *)segue 
{ 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"MMMM dd, yyyy"]; 

    AG_AddItemViewController *source = [segue sourceViewController]; 
    AG_Storage *item = source.store; 

    NSDate *dateCreated = item.creationDate; 



    NSString *todayString = [dateFormat stringFromDate:self.todayDate]; 
    NSString *dateCreatedString = [dateFormat stringFromDate:dateCreated]; 
    NSString *tomorrowString = [dateFormat stringFromDate:self.tomorrowsDate]; 
    NSString *yesterdayString = [dateFormat stringFromDate:self.yesterdaysDate]; 



    //Set up file storage! 



    if (item != nil) { 


     if ([dateCreatedString isEqualToString:todayString]) { 
      [self.mainArray addObject:item]; 
      [tableView reloadData]; 


      NSData *data = [NSKeyedArchiver archivedDataWithRootObject:item.itemName]; 

      [[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:@"%@%d",todayString,self.counter]]; 
      [[NSUserDefaults standardUserDefaults] synchronize]; 


     } 
     else if ([dateCreatedString isEqualToString:tomorrowString]){ 
      [self.tomorrowArray addObject:item]; 
      [tableView reloadData]; 

      NSLog(@"THIS WORKED TOO :D"); 
     } 
     else if ([dateCreatedString isEqualToString:yesterdayString]){ 
      [self.yesterdayArray addObject:item]; 
      [tableView reloadData]; 

      NSLog(@"THIS WORKED"); 
     } 
     else{ 

     } 
    } 
    self.counter++; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableViewer didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableViewer deselectRowAtIndexPath:indexPath animated:NO]; 
    AG_Storage *tappedItem = [self.mainArray objectAtIndex:indexPath.row]; 
    tappedItem.completed = !tappedItem.completed; 
    [tableViewer reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableViews commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"MMMM dd, yyyy"]; 

    NSString *todayString = [dateFormat stringFromDate:self.todayDate]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     //Delete from storage 
     for (int x = 0; x!=-99; x++) { 


      NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%d",todayString,x]]; 
      AG_Storage *someStorageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
      AG_Storage *storeToArray = [[AG_Storage alloc]init]; 
      storeToArray.itemName = someStorageObject; 



      AG_Storage *toDoItem = [self.mainArray objectAtIndex:indexPath.row]; 
      NSString *compare = toDoItem.itemName; 

      //If they equal then delete from NSUserDefaults 
      if ([compare isEqualToString:someStorageObject]) { 

       [[NSUserDefaults standardUserDefaults]removeObjectForKey:[NSString stringWithFormat:@"%@%d",todayString,x]]; 
       [[NSUserDefaults standardUserDefaults]synchronize]; 
       x=-100; 
      } 
      else 
      { 

      } 
      if (x>10) { 
       x=-100; 
      } 


     } 

     [self.mainArray removeObjectAtIndex:indexPath.row]; 
     [tableViews deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 


    } 
} 

@end 

我永遠只能設置一個表的時間,所以我怎麼做yesterdayTableView成爲人口例如yesterdayArray

回答

0

幾種方法這樣做,

如果你只需要在同一時間,你可以做這樣的事情在你的tableView:cellForRowAtIndexPath:數據源的方法來顯示一個的tableView:

的笨方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (kAppState_Yesterday) { // Set the controllerstate so that it knows that the arrayDatasource should be yesterday 
     // create and return cell corresponding to yesterdayArray. 
    } 
} 

如果你所有的單元看起來都是一樣的,你可以有一個NSArray *dataSource並將它分配給不同的數組。

的「有點更聰明的方式」

if userClicksButton `display yesterday array` 

    self.dataSource = self.yesterdayArray 

end 

這樣一來,你的tableView:cellForRowAtIndexPath:將更加一致,因爲如果不充滿條件句。

如果事情變得更加複雜,我會推薦使用Controller Containment。

2

你可以保留一個tableView。

的步驟,如果通過其他條件上的一些按鈕tableView:cellForRowAtIndexPath的點擊其中的tableView多個電池根據自己的需要

1)昨天和明天的筆尖設計單獨的電池/分鏡
2)交換電池。

要不然去看viewController遏制

相關問題