简体   繁体   中英

How do I update a view's content when a UITableView is rearranged?

Here's a run down of what I want to accomplish:

  • Main UIViewController is a UITableViewController
  • Tapping on a cell takes user to a new UIViewController where they can input text and save it
  • User can preview all the text saved on another UIViewController
  • When user re-arranges the table in the Main UIViewController , user can see the changes in the Preview

How can I change/move the text on the Preview UIViewController when the UITableViewController is re-arranged?

Any general approach, tips, tutorials, examples is much appreciated!

Thanks!

EDIT, Here's my code:

Singleton .h

 #import <Foundation/Foundation.h>

 @interface MyInformation : NSObject
 {
     NSMutableArray * informationArray;
     NSMutableString * nameString;
     NSMutableString * jobString;
 }

 @property (nonatomic, retain) NSMutableArray * informationArray;
 @property (nonatomic, retain) NSMutableString * nameString;
 @property (nonatomic, retain) NSMutableString * jobString;

 + (id)sharedInformation;

 @end

Singleton .m

 #import "MyInformation.h"

 @implementation MyInformation

 static MyInformation * _sharedMyInformation = nil;

 @synthesize informationArray = _informationArray;
 @synthesize nameString = _nameString;
 @synthesize jobString = _jobString;

 #pragma mark Singleton Methods

 + (id)sharedInformation
 {
     @synchronized(self)
     {
         if (_sharedMyInformation == nil)
         {
             _sharedMyInformation = [[self alloc] init];
         }   
     }
     return _sharedMyInformation;
 }

 - (id)init
 {
     if (self = [super init])
     {
         _nameString = [[NSMutableString alloc] init];
         _jobString = [[NSMutableString alloc] init];
         _informationArray = [[NSMutableArray alloc] initWithObjects:_nameString, _jobString, nil];

     }
     return self;
 }

 @end

ViewController.m

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     if ([[UIScreen mainScreen] bounds].size.height == 568)
     {
         self.aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 1136) style:UITableViewStyleGrouped];
         self.aTableView.delegate = self;
         self.aTableView.dataSource = self;
         [self.view addSubview:self.aTableView];

         self.editBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editButtonTapped)];
         self.navigationItem.rightBarButtonItem = self.editBarButtonItem;
     }
 }

 - (void)viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:animated];

     [self.aTableView reloadData];
 }

 - (void)editButtonTapped
 {
      if (self.editing)
      {
          [super setEditing:NO animated:NO];
          [self.aTableView setEditing:NO animated:NO];
          [self.aTableView reloadData];
          [self.editBarButtonItem setTitle:@"Edit"];
          [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
       }
       else
       {
          [super setEditing:YES animated:YES];
          [self.aTableView setEditing:YES animated:YES];
          [self.aTableView reloadData];
          [self.editBarButtonItem setTitle:@"Done"];
          [self.editBarButtonItem setStyle:UIBarButtonItemStyleDone];
       }
   }

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return 2;
  }

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      MyInformation * myInformation = [MyInformation sharedInformation];
      int count = [myInformation.informationArray count];

      if (section == 0)
      {
          return count;
      }
      else if (section == 1)
      {
          return 1;
      }

      return count;
  }

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString * CellIdentifier = @"CellIdentifier";

     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

      if (cell == nil)
      {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }

     MyInformation * myInformation = [MyInformation sharedInformation];

     if (indexPath.section == 0 && indexPath.row == 0)
     {
         cell.textLabel.text = [NSString stringWithFormat:@"%@", myInformation.nameString];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }

     if (indexPath.section == 0 && indexPath.row == 1)
     {
         cell.textLabel.text = [NSString stringWithFormat:@"%@", myInformation.jobString];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }

     if (indexPath.section == 1 && indexPath.row == 0)
     {
         cell.textLabel.text = @"Preview";
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }

     return cell;
 }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     [tableView deselectRowAtIndexPath:indexPath animated:YES];

      if (indexPath.section == 0 && indexPath.row == 0)
      {
         NameViewController * name = [[NameViewController alloc] initWithNibName:@"NameViewController" bundle:nil];
         [self.navigationController pushViewController:name animated:YES];
      }

     if (indexPath.section == 0 && indexPath.row == 1)
     {
         JobViewController * job = [[JobViewController alloc] initWithNibName:@"JobViewController" bundle:nil];
         [self.navigationController pushViewController:job animated:YES];    
     }

     if (indexPath.section == 1 && indexPath.row == 0)
     {
         PreviewViewController * preview = [[PreviewViewController alloc] initWithNibName:@"PreviewViewController" bundle:nil];
          [self.navigationController pushViewController:preview animated:YES];
    }
 }


 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
 {
     MyInformation * myInformation = [MyInformation sharedInformation];

     NSString * item = [myInformation.informationArray objectAtIndex:sourceIndexPath.row];
     [myInformation.informationArray removeObjectAtIndex:sourceIndexPath.row];
     [myInformation.informationArray insertObject:item atIndex:destinationIndexPath.row];
 }

 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if (indexPath.section == 0 )
     {
         return YES;
     }   
     else
     {
         return NO;  
     }
 }
 @end

EDIT 2:

I do not believe my array is getting updated.

Here's an example of my save code:

 - (IBAction)saveButtonTapped
 {
     MyInformation * myInformation = [MyInformation sharedInformation];

     if (_nameTextField.text == nil)
     {
         myInformation.nameString = @"";
     } 
     else
     {
         [myInformation.nameString setString:@""];
         [myInformation.nameString appendFormat:@"%@", _nameTextField.text];
     }

     [self.navigationController popToRootViewControllerAnimated:YES];
 }

In this log, the nameString does update to whatever text I type in and save.

But my informationArray always returns as ( "", "" )

EDIT 3 & 4:

Hard coding the strings in the singleton with set characters.

Now I can rearrange my table which is good.

Below is the updated code. The labels on the preview page will rearrange when I rearrange the table.

Here's that code:

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     MyInformation * myInformation = [MyInformation sharedInformation];

     _nameLabel = [[UILabel alloc] init];
     _nameLabel.frame = CGRectMake(20, 20, 300, 44);
     _nameLabel.text = [NSString stringWithFormat:@"%@", [myInformation.informationArray objectAtIndex:0]];

     _jobLabel = [[UILabel alloc] init];
     _jobLabel.frame = CGRectMake(50, 50, 300, 44);
     _jobLabel.text = [NSString stringWithFormat:@"%@", [myInformation.informationArray objectAtIndex:1]];

     [self.view addSubview:_nameLabel];
     [self.view addSubview:_jobLabel];
 }

EDIT 5:

The only thing left to accomplish is how do I update the text in the strings in the singleton and in the UITableViewCells?

EDIT 6:

I solved the issue. I had to change the strings in the Singleton to NSMutableStrings. I modified my save button code to set the string to an empty value and the append it with the textfield text. After this it now works as expected!

Thank you for the help and suggestions!

I am assuming you have a backing NSMutableArray , but the answer should generalize to any data storage scheme.

  1. Use tableView:moveRowAtIndexPath:toIndexPath: to detect when the user reorders the first table and update the backing array by removing the object at fromIndexPath.row and inserting it at toIndexPath.row . This makes sure that your backing models accurately reflect the changes the user just made in the UI.

  2. When the user moves to the preview view, that view either needs to pull from the same shared data source (the one we modified in #1) or be given a copy of the just rearranged array. The latter case is simpler and can be done by adding an NSArray property on the preview controller: @property(nonatomic, copy) NSArray *itemsToPreview . Then ensure that the preview controller uses that array instead of the original one.

Apple's documentation on reordering table views is a good resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM