2012-03-23 85 views
0

我只是新的iOS编程,所以感谢任何帮助。我试图保存从UITextField文本plist并加载它在UITableView中。加载plist在UITableView

我已经设法使文本不被覆盖,但是当我尝试将它加载到表中时,应用程序崩溃。

我知道问题是当我的表加载Data.plist,因为它是数组中的数组。

但我仍然不知道如何加载我的plist到表。 这是我的代码将文本保存到plist。

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsPath = [paths objectAtIndex:0]; 

    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 

    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath]; 


    if (nil == array) { 
     array = [[NSMutableArray alloc] init]; 
    } 


    NSMutableArray *list = [[NSMutableArray alloc]init]; 

    [list addObject:resi.text]; 

    [array addObject:list]; 

    [array writeToFile:plistPath atomically: TRUE]; 

这是我的表视图整个代码

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
    } 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
} 

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (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 [array count]; 
    } 

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

// Configure the cell... 
cell.textLabel.text = [self.array objectAtIndex:indexPath.row]; 

//cell.textLabel.text = [array objectAtIndex:indexPath.row]; 

return cell; 
} 

- (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]; 
*/ 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
// get paths from root direcory 
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path 
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our Data/plist file 
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 

array = [NSMutableArray arrayWithContentsOfFile:plistPath]; 

[myHistoryTable reloadData]; 

} 

回答

2
cell.textLabel.text = [self.array objectAtIndex:indexPath.row]; 

试试这个

//As you have NSArrays as objects in self.array. 
NSArray *list = (NSArray *)[self.array objectAtIndex:indexPath.row]; 
if(list && [list count] > 0) { //to check and avoid any crash 
    cell.textLabel.text = [list objectAtIndex:0]; 
} 
+0

感谢拉胡尔·夏尔马 – Krrish 2012-03-23 08:47:16

+1

谢谢..!这works..but附加问题,如果我只想列出10个列表呢?如果有11个项目,则第一个项目将被第二个项目替换,等等。 – 2012-03-23 17:43:11