[英]How do I update a view's content when a UITableView is rearranged?
Here's a run down of what I want to accomplish: 这是我要完成的工作的摘要:
UIViewController
is a UITableViewController
主UIViewController
是一个UITableViewController
UIViewController
where they can input text and save it 轻触单元格会将用户带到新的UIViewController
,他们可以在其中输入文本并将其保存 UIViewController
用户可以预览保存在另一个UIViewController
上的所有文本 UIViewController
, user can see the changes in the Preview 当用户在Main UIViewController
重新排列表时,用户可以在Preview中看到更改 How can I change/move the text on the Preview UIViewController
when the UITableViewController
is re-arranged? 重新排列UITableViewController
时,如何更改/移动Preview UIViewController
上的文本?
Any general approach, tips, tutorials, examples is much appreciated! 任何通用的方法,技巧,教程,示例都非常感谢!
Thanks! 谢谢!
EDIT, Here's my code: 编辑,这是我的代码:
Singleton .h 单例.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 单例.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 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: 编辑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. 在此日志中,nameString确实更新为我键入并保存的任何文本。
But my informationArray always returns as ( "", "" ) 但是我的informationArray总是返回为(“”,“”)
EDIT 3 & 4: 编辑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: 编辑5:
The only thing left to accomplish is how do I update the text in the strings in the singleton and in the UITableViewCells? 剩下要做的唯一事情就是如何更新单例和UITableViewCells中的字符串中的文本?
EDIT 6: 编辑6:
I solved the issue. 我解决了这个问题。 I had to change the strings in the Singleton to NSMutableStrings. 我不得不将Singleton中的字符串更改为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. 我假设您有NSMutableArray
的支持,但是答案应该概括为任何数据存储方案。
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
. 使用tableView:moveRowAtIndexPath:toIndexPath:
来检测用户何时重新排序第一个表并通过从fromIndexPath.row
删除对象并将其插入toIndexPath.row
更新后备数组。 This makes sure that your backing models accurately reflect the changes the user just made in the UI. 这可以确保您的支持模型准确反映用户刚刚在UI中所做的更改。
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. 当用户移至预览视图时,该视图要么需要从相同的共享数据源(我们在#1中修改过的数据源)中提取,要么需要获得刚刚重新排列的数组的副本。 The latter case is simpler and can be done by adding an NSArray
property on the preview controller: @property(nonatomic, copy) NSArray *itemsToPreview
. 后一种情况更简单,可以通过在预览控制器上添加NSArray
属性来完成: @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. Apple的有关对表视图重新排序的文档是一个很好的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.