简体   繁体   English

在UITableView中加载plist

[英]Load plist in UITableView

i'm just new to iOS Programming, so appreciate for any help. 我只是iOS编程的新手,所以感谢任何帮助。 I'm trying to save text from UITextField to plist and load it in UITableView. 我正在尝试将文本从UITextField保存到plist并将其加载到UITableView中。

I've managed to make the text not overwritten, but when i try to load it in the table,the app crash. 我设法使文本没有被覆盖,但是当我尝试在表中加载它时,应用程序崩溃了。

I know the issue is when my table load the Data.plist since it's array in array. 我知道问题是我的表加载Data.plist,因为它是数组中的数组。

But I still don't have to clue how to load my plist to table. 但我仍然不必知道如何将我的plist加载到桌子上。 This is my code when save the text to 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];

And this is the whole code for my table view 这是我的表视图的整个代码

- (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];

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

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM