0

我正在尝试创建一个UITableViewController允许用户UIButton在导航栏中按 a 的方法,输入他们的数据,即名字和姓氏,然后控制器将此数据放入新的UITableViewCell. 我已经为此工作了大约一个星期,似乎有无穷无尽的问题。我将非常感谢有人帮助我完成它,因为我到处搜索但无济于事。

到目前为止,我有以下内容:

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

4

0 回答 0