UITableViewController
ユーザーがUIButton
ナビゲーションバーでaを押し、データ(姓名)を入力すると、コントローラーがこのデータを新しいに配置できるようにするを作成しようとしていますUITableViewCell
。私はこれに約1週間取り組んできましたが、一見無限の問題があります。ウォークスルーを見つけるのに役立たずにどこでも検索したので、誰かが私をそれを通して案内してくれることを大いに感謝します。
私はこれまでに次のものを持っています:
guestlistViewController.h
#import <UIKit/UIKit.h>
@class addGuestViewController;
@interface guestlistViewController : UITableViewController {
NSArray *fullname;
}
@property (nonatomic, strong) addGuestViewController *addGuestViewController;
@end
guestlistViewController.m
#import "guestlistViewController.h"
#import "addGuestViewController.h"
@interface guestlistViewController () {
NSMutableArray *_objects;
}
@property (nonatomic, strong) NSArray *tableData;
@end
@implementation guestlistViewController
@synthesize tableData;
- (void)awakeFromNib
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.addGuestViewController = (addGuestViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
NSArray *array = [[NSArray alloc] initWithObjects:fullname, nil];
self.tableData = array;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
[self performSegueWithIdentifier:@"insertGuest" sender:self];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSArray *array = [[NSArray alloc] initWithObjects:fullname, nil];
NSUInteger row = [indexPath row];
cell.textLabel.text = [array objectAtIndex:row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSArray *array = _objects[indexPath.row];
self.addGuestViewController.detailItem = array;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"addGuest"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
addGuestlistViewController.h
#import <UIKit/UIKit.h>
@interface addGuestViewController : UITableViewController <UISplitViewControllerDelegate>
@property (nonatomic, strong) id detailItem;
@property (retain) NSString *fullname;
@end
addGuestlistViewController.m
#import "addGuestViewController.h"
#import "guestlistViewController.h"
@interface addGuestViewController () <UITextFieldDelegate>
@property (nonatomic, strong) NSArray *guestTable;
@property (nonatomic, strong) UITextField *firstname;
@property (nonatomic, strong) UITextField *lastname;
@property (nonatomic, strong) UIPopoverController *masterPopoverController;
- (void)configureView;
@end
@implementation addGuestViewController
@synthesize guestTable;
@synthesize firstname;
@synthesize lastname;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
newDetailItem = [[NSString alloc] initWithFormat:@"%@ %@", firstname, lastname];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
if (self.detailItem) {
self.fullname = [self.detailItem description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *addGuest = [[NSArray alloc] initWithObjects:@"", @"", nil];
self.guestTable = addGuest;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberofSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.guestTable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath row] == 0) {
firstname = [[UITextField alloc] initWithFrame:CGRectMake(30, 10, 280, 30)];
firstname.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
firstname.adjustsFontSizeToFitWidth = YES;
firstname.placeholder = @"First Name";
firstname.textColor = [UIColor blackColor];
firstname.returnKeyType = UIReturnKeyNext;
firstname.delegate = self;
firstname.autocorrectionType = UITextAutocorrectionTypeNo;
firstname.tag = 0;
firstname.clearButtonMode = UITextFieldViewModeWhileEditing;
[firstname setEnabled: YES];
[cell addSubview:firstname];
[firstname becomeFirstResponder];
} else if ([indexPath row] == 1) {
lastname = [[UITextField alloc] initWithFrame:CGRectMake(30, 10, 280, 30)];
lastname.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
lastname.adjustsFontSizeToFitWidth = YES;
lastname.placeholder = @"Last Name";
lastname.textColor = [UIColor blackColor];
lastname.returnKeyType = UIReturnKeyDone;
lastname.delegate = self;
lastname.autocorrectionType = UITextAutocorrectionTypeNo;
lastname.tag = 0;
lastname.clearButtonMode = UITextFieldViewModeWhileEditing;
[lastname setEnabled: YES];
[cell addSubview:lastname];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [guestTable objectAtIndex:row];
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == firstname) {
[textField resignFirstResponder];
[lastname becomeFirstResponder];
} else if (textField == lastname) {
[textField resignFirstResponder];
[self performSegueWithIdentifier:@"done" sender:self];
_fullname = [[NSString alloc] initWithFormat:@"%@ %@", firstname.text, lastname.text];
NSLog(@"Detail pulled off name of: %@", _fullname);
}
return YES;
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)splitController willHideTableViewController:(UITableViewController *)tableViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"Master", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowTableViewController:(UITableViewController *)tableViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
@end
私が試したさまざまな方法の数はかなり確かです。それはいくつかの異なる方法の大きな混合物です。私はiOSプログラミングに本当に慣れていないので、うまくいけばそれは回収可能です:P