簡體   English   中英

使用AlertPrompt時重新加載tableView

[英]reload tableView when using AlertPrompt

(已解決)當我使用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

任何幫助,將不勝感激!

如果您的類實際上是UITableViewController,那么您只需訪問self.tableView即可訪問實現中需要它的任何地方的tableView對象。 我想念什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM