Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 UITableview在重新加载时出现问题_Iphone_Objective C_Uitableview - Fatal编程技术网

Iphone UITableview在重新加载时出现问题

Iphone UITableview在重新加载时出现问题,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我已经完成了一个学校项目的申请,但遇到了一个重大的“错误”。 这是一个帐户管理应用程序。图片: 当我点击加号时,我按下导航控制器来加载另一个视图,以处理类别的添加和删除。当我添加并返回到上面的视图时,它不会更新。它只有在我点击右边的按钮后才会更新,这是另一个用来更改某些设置的视图,然后返回页面。我做了一些关于ViewWillExample之类的研究,但我仍然不明白为什么它不能正常工作 当我删除一个类别并返回到这个视图时,这个问题也会影响我的程序。它会崩溃,因为视图没有成功重新加载。在删除并返回

我已经完成了一个学校项目的申请,但遇到了一个重大的“错误”。 这是一个帐户管理应用程序。图片:

当我点击加号时,我按下导航控制器来加载另一个视图,以处理类别的添加和删除。当我添加并返回到上面的视图时,它不会更新。它只有在我点击右边的按钮后才会更新,这是另一个用来更改某些设置的视图,然后返回页面。我做了一些关于ViewWillExample之类的研究,但我仍然不明白为什么它不能正常工作

当我删除一个类别并返回到这个视图时,这个问题也会影响我的程序。它会崩溃,因为视图没有成功重新加载。在删除并返回视图时,我将出现此错误。“*由于未捕获的异常“NSRangeException”而终止应用程序,原因:”*-[NSMutableArray objectAtIndex:]:索引4超出边界[0..3]”


[编辑]

表视图代码:

@class LoginViewController;

@implementation CategoryTableViewController
@synthesize categoryTableViewController;
@synthesize categoryArray;
@synthesize accountsTableViewController;
@synthesize editAccountTable;
@synthesize window;

CategoryMgmtTableController *categoryMgmtTableController;
ChangePasswordView *changePasswordView;

- (void) save_Clicked:(id)sender {
/*
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Category Management" 
                                                message:@"Load category management table view"
                                               delegate:self 
                                      cancelButtonTitle: @"OK" 
                                      otherButtonTitles:nil];
[alert show];
[alert release];
*/
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
categoryMgmtTableController = [[CategoryMgmtTableController alloc]initWithNibName:@"CategoryMgmtTable" bundle:nil]; 
[appDelegate.categoryNavController pushViewController:categoryMgmtTableController animated:YES];



}
- (void) change_Clicked:(id)sender {

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Change Password" 
 message:@"Change password View"
 delegate:self 
 cancelButtonTitle: @"OK" 
 otherButtonTitles:nil];
 [alert show];
 [alert release];

KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
changePasswordView = [[ChangePasswordView alloc]initWithNibName:@"ChangePasswordView" bundle:nil]; 
[appDelegate.categoryNavController pushViewController:changePasswordView animated:YES];


/*
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
categoryMgmtTableController = [[CategoryMgmtTableController alloc]initWithNibName:@"CategoryMgmtTable" bundle:nil]; 
[appDelegate.categoryNavController pushViewController:categoryMgmtTableController animated:YES];
*/


}
#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if ((self = [super initWithStyle:style])) {
}
return self;
}
*/



-(void) initializeCategoryArray {

sqlite3 *db= [KeyCryptAppAppDelegate getNewDBConnection];
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];

const char *sql = [[NSString stringWithFormat:(@"Select Category from Categories;")]cString];

const char *cmd = [[NSString stringWithFormat:@"pragma key = '%@' ", appDelegate.pragmaKey]cString];
sqlite3_stmt *compiledStatement;

sqlite3_exec(db, cmd, NULL, NULL, NULL);

if (sqlite3_prepare_v2(db, sql, -1, &compiledStatement, NULL)==SQLITE_OK)
{
    while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        [categoryArray addObject:[NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement, 0)]];

}
else {
    NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(db));
}
sqlite3_finalize(compiledStatement);
}



#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[super viewDidLoad];
}



- (void)viewWillAppear:(BOOL)animated {

self.title = NSLocalizedString(@"Categories",@"Types of Categories");
categoryArray = [[NSMutableArray alloc]init];
[self initializeCategoryArray];

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                           target:self action:@selector(save_Clicked:)] autorelease];

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                          target:self action:@selector(change_Clicked:)] autorelease];

[super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated {
NSLog (@"view did appear");
[super viewDidAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated {

NSLog (@"view will disappear");
[super viewWillDisappear:animated];
}


- (void)viewDidDisappear:(BOOL)animated {
[categoryTableView reloadData];
NSLog (@"view did disappear");
[super viewDidDisappear:animated];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


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


// Customize the appearance of table view cells.
- (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] autorelease];
}

// Configure the cell...
NSUInteger row = [indexPath row];
cell.text = [categoryArray objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

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 {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}   
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
}   
}
*/


/*
// 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 -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCategory = [categoryArray objectAtIndex:[indexPath row]]; 

NSLog (@"AccountsTableView.xib is called.");
if ([categoryArray containsObject: selectedCategory])
{
    if (self.accountsTableViewController == nil)
    {
        AccountsTableViewController *aAccountsView = [[AccountsTableViewController alloc]initWithNibName:@"AccountsTableView"bundle:nil];
        self.accountsTableViewController =aAccountsView;
        [aAccountsView release];
    }

    NSInteger row =[indexPath row];
    accountsTableViewController.title = [NSString stringWithFormat:@"%@", [categoryArray objectAtIndex:row]];

    // This portion pushes the categoryNavController.
    KeyCryptAppAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [self.accountsTableViewController initWithTextSelected:selectedCategory];
    KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.pickedCategory = selectedCategory;
    [delegate.categoryNavController pushViewController:accountsTableViewController animated:YES];

}

 }  



#pragma mark -
#pragma mark Memory management

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

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

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
[accountsTableViewController release];
[super dealloc];
}


@end
以及我用来删除行的代码(这是在一个完全不同的tableview中):


在Tableview CommittedItingStyle方法中..必须首先从类别数组中删除对象,然后删除行(&R)。 通过将用于从数组中删除对象的代码放在最后一行来更改代码

即。 将以下类型的代码放在最后
[categoryArray removeObjectAtIndex:indexPath.row]

仅从数组中删除项不会更新表。您需要调用reloadData来更新tableview,或者更好的是,调用tableview上的方法,这些方法允许您插入或删除带有动画的行,而无需重新加载整个表

insertRowsAtIndexPaths:withRowAnimation:
deleteRowsAtIndexPaths:withRowAnimation:

您需要在ViewWillDisplay中重新加载表格

通过添加此行

[yourTable reloadData];
因为表格委托函数在重新出现屏幕时未得到调用。 但当你们再次转到下一个视图时,它们会调用,这就是为什么保存后你们不会在表中得到更新的信息,但你们会在更多的导航中得到更新,因为这次在视图中重新加载数据调用将消失


为了解决删除问题,您需要按照iphoneDev ans和说明进行操作。

您需要给出表视图的代码。删除后,您还需要从数组中删除该对象。虽然删除了一个对象,但数组计数仍然保持在4。希望这对您有所帮助如果这对您没有帮助,只需按照Ishu的建议为tableView编写代码即可。嗨,我已经给出了tableView的代码以及用于删除我的类别的代码(不过这是在不同的tableView中)。谢谢你的邀请help@ishu,在代码中-(void)视图将消失:(BOOL)动画{表已重新加载。由于数组在删除后未更新,因此出现崩溃。请在删除之前检查代码answering@Swastik,当他来到视图时,他没有得到更新的表,所以这个问题可以通过在视图中重新加载表来解决。所以在评论之前先考虑一下。我不是建议为崩溃重新加载数据。ok。@Swastik:ishu是正确的,seelani应该在视图中重新加载表格。正如她所提到的,添加新类别后,当视图出现时,新类别不会显示。所以她应该在视图出现时重新加载表格&而不是消失时。嘿,伙计们,我真的很高兴你们在“战斗”给我一个正确的答案,但假设我将所有正常运行的方法和准备视图移动到viewdidload,并输入[myTable reloadData]在viewwillappear中,问题没有解决,而且看起来比以前更糟,我必须在更改可见之前退出应用程序。我讨厌在晚上8点时无法找出代码的错误…但在早上1:30修复它没有问题…-----“我只需在viewwillappear[自初始化CountStableRay];[accountsTableView重新加载数据];
[yourTable reloadData];