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];
}
あなたの質問はちょっと混乱しており、詳細が必要です。私はあなたがあなたのテーブルに***最後の***アイテムを表示するだけでいいと思っています。とにかくこのplistを生成したのは何ですか?あなたがファイルに書き込んだのは単に 'NSDictionary'か' NSArray'ですか? –
こんにちはマイケル、はい私は私のテーブルがUITextFieldからの最後の10項目とその実際の配列を表示するようにしたい、これはコードですNSMutableArray * list = [[NSMutableArray alloc] init]; [list addObject:resi.text]; [配列addObject:リスト]; [配列writeToFile:plistPathは原子的に:TRUE]; –