UITableview имеет проблемы с перезагрузкой - PullRequest
1 голос
/ 22 декабря 2010

Я вроде как закончил заявку на школьный проект, но столкнулся с серьезной «ошибкой». Это приложение для управления аккаунтом. Фото:

alt text

Вот проблема, когда я нажимаю на знак плюс, я нажимаю навигационный контроллер, чтобы загрузить другое представление для обработки добавления и удаления категорий. Когда я добавляю и возвращаюсь к представлению выше, оно не обновляется. Он обновляется только после того, как я нажму кнопку справа, которая представляет собой другой вид, используемый для изменения некоторых настроек и возврата на страницу. Я провел некоторые исследования ViewWillAppear и тому подобное, но я все еще не понимаю, почему он не работает должным образом.

Эта проблема также влияет на мою программу, когда я удаляю категорию и возвращаюсь к этому представлению, оно вылетает, потому что представление не было перезагружено успешно. Я получу эту ошибку при удалении и возвращении к представлению. "* Завершение работы приложения из-за необработанного исключения 'NSRangeException', причина: '* - [NSMutableArray objectAtIndex:]: индекс 4 за пределами [0 .. 3]'".


[EDIT]

Код табличного представления:

@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

И код, который я использовал для удаления строк (это совершенно другое представление таблицы):

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    NSString *selectedCategory = [categoryArray objectAtIndex:indexPath.row];
    [categoryArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];


    [deleteCategoryTable reloadData];

    //NSString *selectedCategory = [categoryArray objectAtIndex:indexPath.row];

    sqlite3 *db= [KeyCryptAppAppDelegate getNewDBConnection];
    KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    const char *sql = [[NSString stringWithFormat:@"Delete from Categories where Category = '%@';", selectedCategory]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)
    {
        sqlite3_exec(db,sql,NULL,NULL,NULL);

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



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
}   
}

Ответы [ 3 ]

0 голосов
/ 22 декабря 2010

Простое удаление элементов из массива не приведет к обновлению таблицы.Вам нужно вызвать reloadData для обновления табличного представления или, что еще лучше, вызвать методы табличного представления, которые позволяют вставлять или удалять строки с анимацией без перезагрузки всей таблицы.

insertRowsAtIndexPaths:withRowAnimation:
deleteRowsAtIndexPaths:withRowAnimation:
0 голосов
/ 22 декабря 2010

Вам нужно перезагрузить таблицу в viewWillappear

добавив эту строку

[yourTable reloadData];

, поскольку функция делегирования таблиц не получает вызов, и снова появляется экран. Но они вызываются, когда вы снова переходите к следующему представлению, поэтому после сохранения вы не получаете обновленную информацию в таблице, а получаете дополнительную навигацию, потому что на этот раз вызовы reloadData вызывают в viewWillDisappear.

и для решения проблемы удаления необходимо следовать инструкции и инструкции iphoneDev.

0 голосов
/ 22 декабря 2010

В методе TableView commitEditingStyle ... сначала необходимо удалить объект из массива категорий, а затем удалить строку.Измените свой код, поместив код для удаления объекта из массива в последний.

т.е.поместите следующий тип кода в последний [categoryArray removeObjectAtIndex: indexPath.row]

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...