iphone - 在 UITableView 中加载 plist

标签 iphone ios xcode cocoa-touch plist

我只是 iOS 编程的新手,非常感谢您的帮助。我正在尝试将 UITextField 中的文本保存到 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];

这是我的表格 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];

试试这个

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

关于iphone - 在 UITableView 中加载 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9834272/

相关文章:

iphone - 应用程序并不总是在主视图中启动

iphone - iPhone模拟器生成的崩溃日志?

php - 如何在 IOS 上使用 Swift 解析从 PHP 服务脚本发送的 JSON?

ios - PHFetchResults 日期过滤器没有为时间范围生成正确的结果

xcode - 增加 Xcode "recent project"列表的长度?

iphone - iOS 如何将可重用代码打包到可重用的 UI 小部件中?像经过大量修改的文本小部件?

ios - 类型 'UIStackView' 的值没有成员 'delegate' - Swift 初学者指南

xcode - IBOutlets 未显示

android - IOS - 从 Parse.com 后端下载图像

ios - SKPhysicsContactDelegate 协议(protocol)方法未被调用