Вот бежать вниз, что я хочу сделать:Как обновить содержимое представления, когда UITableView перестроен?
- Главная
UIViewController
являетсяUITableViewController
- При нажатии на ячейку требуется пользователю новый
UIViewController
где можно ввести текст и сохранить его - Пользователь может просмотреть весь текст, сохраненный на другом
UIViewController
- Когда пользователь повторно упорядочивает таблицу в главном
UIViewController
, пользователь может увидеть изменения в окне предварительного просмотра
Как изменить или переместить текст в окне предварительного просмотра UIViewController
, когда UITableViewController
переустановлен?
Любой общий подход, советы, учебники, примеры очень ценятся!
Спасибо!
EDIT, Вот мой код:
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:
Я не верю, что мой массив обновляется.
Вот пример моего кода сохранения:
- (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 всегда возвращается, как ("", "")
EDIT 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];
}
EDIT 5:
Единственное, что осталось выполнить то, как обновить текст в строках в одноэлементных и в UITableViewCells?
EDIT 6:
Я решил проблему.Мне пришлось менять строки в Singleton на NSMutableStrings. Я изменил код кнопки сохранения, чтобы установить строку в пустое значение и добавить ее с текстом текстового поля. После этого он работает так, как ожидалось!
Благодарим за помощь и предложения!
Пытается найти общее направление, как подойти к этому. Не знаю, как это сделать в данный момент. –
Итак, как таблица, так и представления просмотра отображаются одновременно на экране, и вы хотите обновить предварительный просмотр ячейки в верхней части таблицы. – Wain
Нет, основной вид представляет собой табличный вид. Нажатие на ячейку переходит на страницу подробностей для ввода текста. Нижняя ячейка укажет на страницу предварительного просмотра. Пользователь может изменить таблицы, которые ведут к подробным представлениям с текстом. Я хочу, чтобы предварительный просмотр оставался обновленным в таблице, поэтому он перечисляет текст в том же порядке. –