2012-03-23 9 views
0

iOSプログラミングの新機能です。ご協力いただきありがとうございます。私は、UITextFieldからテキストをplistに保存し、UITableViewに読み込もうとしています。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

+1

を試しているが、ありがとう...!それは追加のworks..but質問、私はちょうど10だけリストを作りたい場合はどうすればいいですか? 11の項目がある場合、最初の項目は2番目の項目に置き換えられます。 –

関連する問題