Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用AlertPrompt时重新加载tableView_Iphone_Objective C_Ios4_Tableview - Fatal编程技术网

Iphone 使用AlertPrompt时重新加载tableView

Iphone 使用AlertPrompt时重新加载tableView,iphone,objective-c,ios4,tableview,Iphone,Objective C,Ios4,Tableview,解决了在使用AlertPrompt接受用户输入时重新加载tableView时遇到的一些问题。主要是,我似乎无法将UITableView传递给读取用户输入的方法,这使得在我输入新数据时无法重新加载tableView。现在一切正常,我只需要在数据删除或其他情况下重新加载tableView。这是我的代码tableViewController #import "GroceryListTableViewController.h" #import "AlertPrompt.h" @implementat

解决了在使用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对象。我遗漏了什么?

很抱歉,我只是想问一下iPhone开发人员的问题,但问题正是你说的-我忘记添加self.tableView重载数据我的代码就是tableView重载数据。我没有意识到缺少自引用会阻止我使用变量。它有点依赖于上下文,尤其是在UITableView数据源和委托方法中。还有tableView参数,它可以屏蔽self.tableView实例变量。。。有时:这肯定会让人困惑!如果答案是好的,考虑把它标记为这样。谢谢,祝你好运!