当我切换到另一页然后返回时,表视图单元格消失

时间:2011-08-21 23:48:19

标签: iphone objective-c uitableview tableview

我有一个表格视图,您可以添加和删除单元格。我可以输入多个单元格,当我转到下一页然后切换回来时,我的所有条目/单元格都将被删除。任何人都可以搞清楚这一点吗?这是我的代码:

@implementation FacePlatesViewController

@synthesize woodGrain;
@synthesize nav, array;
@synthesize EditButton;
@synthesize myTableView, image;
@synthesize cell, string1;
@synthesize myDic, cells, defaults;
@synthesize selectedCell, currentChosenFund;


- (void)viewDidLoad {
[super viewDidLoad];
NSString * myFile = [[NSBundle mainBundle]pathForResource:@"cells" ofType:@"plist"];
self.myTableView.backgroundColor = [UIColor clearColor];
cells = [[NSMutableArray alloc]initWithContentsOfFile:myFile];

}


- (void)addObject:(id)anObject
{
if (anObject != nil)
{
    [cells addObject:anObject];
}
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self.myTableView reloadData];
}

- (IBAction)editButton:(id)sender 
{




if (self.editing) 
    {
        [self setEditing:NO animated:YES];
        [self.myTableView setEditing:NO animated:YES];

    } 

    else 
    {

        [self setEditing:YES animated:YES];
        [self.myTableView setEditing:YES animated:YES];

    }
}

- (void)add
{
    MyDetailViewController * detail = [[MyDetailViewController alloc]init];
    detail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:detail animated:YES];
    [detail.text becomeFirstResponder];
    [detail release];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [cells count];
}

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [[self cells] removeObjectAtIndex:[indexPath row]];

        NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
        [[self myTableView] deleteRowsAtIndexPaths:indexPaths
                                  withRowAnimation:UITableViewRowAnimationFade];
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   
(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero   
                                       reuseIdentifier:CellIdentifier] autorelease];
    }  
    //The if block should end here. You should set the cell's label irrespective whether the cell was nil. This is the cause of the issue you are facing.

    cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:@"name"];


    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstFolderViewController * first = [[FirstFolderViewController alloc]init];
    first.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:first animated:YES];
    [first release];
}

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
       toIndexPath:(NSIndexPath *)targetIndexPath
{
    NSUInteger sourceIndex = [sourceIndexPath row];
    NSUInteger targetIndex = [targetIndexPath row];

    if (sourceIndex != targetIndex)
    {
        [[self cells] exchangeObjectAtIndex:sourceIndex
                          withObjectAtIndex:targetIndex];
    }

}

- (void)dealloc
{
    [woodGrain release];
    [myTableView release];
    [EditButton release];
    [nav release];
    [cells release];
    [myTableView release];
    [myDic release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];  
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [self setWoodGrain:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

谢谢:D

2 个答案:

答案 0 :(得分:1)

实际上即使卸载视图也足以抛弃细胞。在viewDidLoad中尝试这个:

if(!cells) {
    NSString * myFile = [[NSBundle mainBundle]pathForResource:@"cells" ofType:@"plist"];
    self.myTableView.backgroundColor = [UIColor clearColor];
    cells = [[NSMutableArray alloc]initWithContentsOfFile:myFile];
}

(这样每次加载视图时你都不会重新读取数组。)

如果您希望添加的单元格在应用程序重新启动之间保持不变,则需要将它们保存在某处。您无法更改主包中的文件,但您可以将自己的文件写入Caches文件夹,您可以通过该文件夹获取:

[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

将您的文件写入该文件夹并从那里读取单元格而不是主包。如果主捆绑文件中有一些预定义的单元格,则可以在应用程序启动时检查Caches文件夹中的文件是否存在,如果不存在,则将捆绑包的文件复制到Caches文件夹中。

编辑:如果你使用presentModalViewController从另一个页面返回,你将获得FacePlatesViewController的新副本,它显然会加载文件中的默认单元格。 相反,你应该添加一个"委托"属性到FirstFolderViewController:

@property(nonatomic, assign) id delegate; //yes, assign, not retain

然后在呈现FirstFolderViewController时执行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstFolderViewController * first = [[FirstFolderViewController alloc]init];
    first.delegate = self;
    first.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:first animated:YES];
    [first release];
}

然后向FacePlatesViewController添加一个方法:

- (void) onDoneWithFirstFolderViewController //you can come up with a better name
{
    [self dismissModalViewControllerAnimated:YES];
}

并在您的FirstFolderViewController中,当您准备关闭它时,请执行:

if([delegate respondsToSelector:@selector(onDoneWithFirstFolderViewController)]) 
    [delegate onDoneWithFirstFolderViewController];

具有讽刺意味的是,如果你已经在文件中实现了单元持久性,那么这个问题可能就不会引起注意了(因为每个新的FacePlatesViewController都会加载一个最新的单元格列表),但每次你都会发生内存泄漏介于两页之间。

答案 1 :(得分:0)

对我而言,问题似乎与ViewWillAppear:中的代码有关。当您致电[self.myTableView reloadData];时,您是否确保您的单元格数组正常?

另外,我注意到myTableView。您是继承UITableViewController还是实现表委托?如果您是子类,则表视图引用名为tableView

HTH,

阿克沙伊