gpt4 book ai didi

ios - 在 UITableView 中加载 Plist 仅 10 行

转载 作者:行者123 更新时间:2023-11-28 23:01:55 24 4
gpt4 key购买 nike

其实这是为了跟进我之前的问题 UITableView show only first row .

现在我的问题是我只想查看我的 plist 中的 10 个列表。如果有 11 个项目,第一个项目将被第二个项目替换,依此类推,所以我的列表只有 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];

这是修改后的 TableView 的全部代码

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

最佳答案

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

如果您不想覆盖原始数组,请创建第二个数组作为 tableView 的数据源:

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

关于ios - 在 UITableView 中加载 Plist 仅 10 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9854125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com