使用AlertPrompt时重新加载tableView

时间:2011-04-22 18:15:19

标签: iphone objective-c ios4 tableview

(已解决)当我使用AlertPrompt接受用户输入时,我在重新加载tableView时遇到了一些麻烦。主要是,我似乎无法将UITableView传递给读入用户输入的方法,这使我无法在输入新数据时重新加载tableView(现在一切正常,我只需要重新加载tableView当数据被删除或某事时)。这是我的代码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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

如果你的类实际上是一个UITableViewController,那么只需访问self.tableView就可以访问实现中任何地方的tableView对象。我错过了什么?

相关问题