2012-03-24 1 views
0

これは実際には UITableView show only first rowの前に私の質問をフォローアップすることです。UITableViewでPlistを10行だけロードする

私の問題は、私のplistの10のリストだけを見たいということです。 11個のアイテムがある場合、最初のアイテムは2番目のアイテムに置き換えられますので、私のリストは10個だけです。

そして、これは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]; 

そして、これはあなたがドン場合

- (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 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    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]; 
    } 
    // Configure the cell... 
    return cell; 
} 

- (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]; 
} 
+0

あなたの質問はちょっと混乱しており、詳細が必要です。私はあなたがあなたのテーブルに***最後の***アイテムを表示するだけでいいと思っています。とにかくこのplistを生成したのは何ですか?あなたがファイルに書き込んだのは単に 'NSDictionary'か' NSArray'ですか? –

+0

こんにちはマイケル、はい私は私のテーブルがUITextFieldからの最後の10項目とその実際の配列を表示するようにしたい、これはコードですNSMutableArray * list = [[NSMutableArray alloc] init]; [list addObject:resi.text]; [配列addObject:リスト]; [配列writeToFile:plistPathは原子的に:TRUE]; –

答えて

2
if ([array count] > 10) { 
    array = [array subarrayWithRange:NSMakeRange([array count] - 10, 10)]; 
} 

を変更された私のテーブルビュー」の全体のコードです元の配列を上書きしたくない場合は、tableViewのdataSourceとして機能する2番目のテーブルを作成します。

array = /* load from plist */; 
if ([array count] > 10) { 
    self.dataSourceArray = [array subarrayWithRange:NSMakeRange([array count] - 10, 10)]; 
} 
else { 
    self.dataSourceArray = array; 
} 
関連する問題