重新排列UITableView时如何更新视图的内容?

时间:2013-04-23 15:46:34

标签: ios objective-c uitableview

这是我想要实现的目标:

  • 主要UIViewControllerUITableViewController
  • 点击一个单元格会将用户带到一个新的UIViewController,在那里他们可以输入文本并保存它
  • 用户可以预览另一个UIViewController
  • 上保存的所有文字
  • 当用户在主UIViewController中重新排列表格时,用户可以在预览中看到更改

如果重新安排UIViewController,如何更改/移动预览UITableViewController上的文字?

非常感谢任何一般方法,技巧,教程和示例!

谢谢!

编辑,这是我的代码:

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

编辑2:

我不相信我的阵列会更新。

以下是我的保存代码示例:

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

在此日志中,nameString会更新为我输入的任何文本并保存。

但我的informationArray总是返回(“”,“”)

编辑3&amp; 4:

使用设置字符对单例中的字符串进行硬编码。

现在我可以重新安排好桌子了。

以下是更新的代码。当重新排列表格时,预览页面上的标签将重新排列。

这是代码:

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

编辑5:

唯一要做的就是如何更新单例和UITableViewCells中字符串中的文本?

编辑6:

我解决了这个问题。我不得不将Singleton中的字符串更改为NSMutableStrings。我修改了我的保存按钮代码,将字符串设置为空值,并将其附加到文本字段文本。在此之后它现在按预期工作!

感谢您的帮助和建议!

1 个答案:

答案 0 :(得分:0)

我假设你有一个支持NSMutableArray,但答案应该推广到任何数据存储方案。

  1. 使用tableView:moveRowAtIndexPath:toIndexPath:检测用户何时重新排序第一个表并通过删除fromIndexPath.row处的对象并将其插入toIndexPath.row来更新支持数组。这可确保您的支持模型准确反映用户在UI中所做的更改。

  2. 当用户移动到预览视图时,该视图需要从同一个共享数据源(我们在#1中修改过的那个)中提取,或者获得刚刚重新排列的数组的副本。后一种情况更简单,可以通过在预览控制器上添加NSArray属性来完成:@property(nonatomic, copy) NSArray *itemsToPreview。然后确保预览控制器使用该阵列而不是原始阵列。

  3. Apple's documentation on reordering table views是一个很好的资源。