[英]How do I update a view's content when a UITableView is rearranged?
這是我要完成的工作的摘要:
UIViewController
是一個UITableViewController
UIViewController
,他們可以在其中輸入文本並將其保存 UIViewController
上的所有文本 UIViewController
重新排列表時,用戶可以在Preview中看到更改 重新排列UITableViewController
時,如何更改/移動Preview UIViewController
上的文本?
任何通用的方法,技巧,教程,示例都非常感謝!
謝謝!
編輯,這是我的代碼:
單例.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
單例.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和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。 我修改了保存按鈕代碼,將字符串設置為空值,並將其附加到文本字段文本中。 此后,它現在可以正常工作了!
感謝您的幫助和建議!
我假設您有NSMutableArray
的支持,但是答案應該概括為任何數據存儲方案。
使用tableView:moveRowAtIndexPath:toIndexPath:
來檢測用戶何時重新排序第一個表並通過從fromIndexPath.row
刪除對象並將其插入toIndexPath.row
更新后備數組。 這可以確保您的支持模型准確反映用戶剛剛在UI中所做的更改。
當用戶移至預覽視圖時,該視圖要么需要從相同的共享數據源(我們在#1中修改過的數據源)中提取,要么需要獲得剛剛重新排列的數組的副本。 后一種情況更簡單,可以通過在預覽控制器上添加NSArray
屬性來完成: @property(nonatomic, copy) NSArray *itemsToPreview
。 然后,確保預覽控制器使用該數組而不是原始數組。
Apple的有關對表視圖重新排序的文檔是一個很好的資源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.