简体   繁体   English

使用AlertPrompt时重新加载tableView

[英]reload tableView when using AlertPrompt

(SOLVED) I'm having a bit of trouble with reloading my tableView when I use an AlertPrompt to accept user input. (已解决)当我使用AlertPrompt接受用户输入时,我在重新加载tableView时遇到了一些麻烦。 Mainly, I can't seem to get the UITableView to be passed to the method which reads in the user input, which makes it impossible to reload the tableView when I enter new data (right now everything works, I just have to reload the tableView when data is deleted or something). 主要是,我似乎无法将UITableView传递给读取用户输入的方法,这使得在输入新数据时无法重新加载tableView(现在一切正常,我只需要重新加载tableView删除数据或其他内容时)。 Here's my code the tableViewController. 这是我的代码tableViewController。

#import "GroceryListTableViewController.h"
#import "AlertPrompt.h"


@implementation GroceryListTableViewController


@synthesize groceries;
//@synthesize alert;
//@synthesize label;

/*- (NSMutableArray *) groceries
{
    if(groceries == nil)
        [self addToGroceries:@"Groceries"];
    return groceries;
}*/
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    self.groceries = [NSMutableArray arrayWithObjects:nil];
    //alert = [AlertPrompt alloc];
    [self addToGroceries:@"Add grocery"];
    [self addToGroceries:@"Steaks"];
    [self addToGroceries:@"Steakss"];
    [self addToGroceries:@"Steaksss"];

    if (self) {
    }
    return self;
}

-(void) addToGroceries:(NSString *)grocery
{
    [self.groceries addObject:grocery];
}

- (void)dealloc
{
    [groceries release];
    //[alert release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [addButton release];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void) showPrompt
{
    AlertPrompt *alert = [AlertPrompt alloc];
    alert = [alert initWithTitle:@"Add an item" message:@"Entry new item here" delegate: self cancelButtonTitle:@"Cancel" okButtonTitle:@"Okay"];
    [alert show];
    [alert release];
}


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        [self addToGroceries:entered];
    }
    //[tableView reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.groceries count];
}

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //if(indexPath.row == 0)
    //   return UITableViewCellEditingStyleInsert;
    //else
        return UITableViewCellEditingStyleDelete;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GroceryListCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.textLabel.text = [self.groceries objectAtIndex:indexPath.row];
    // Configure the cell...
    //if(indexPath.row == 0)
        //cell.editingStyle = UITableViewCellEditingStyleInsert;
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}*/


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [groceries removeObjectAtIndex:[indexPath row]];
        // Delete the row from the data source
        //[tableView reloadData];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }   
    //else if (editingStyle == UITableViewCellEditingStyleInsert) {

        //[alert show];
        //[alert release];
      //  [tableView reloadData];
    //}   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end

Any help would be appreciated! 任何帮助,将不胜感激!

If your class is in fact a UITableViewController, then you have access to the tableView object anywhere in the implementation that you should need it simply by accessing self.tableView . 如果您的类实际上是UITableViewController,那么您只需访问self.tableView即可访问实现中需要它的任何地方的tableView对象。 What am I missing? 我想念什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM