gpt4 book ai didi

ios - 从 iOS 中的 plist 文件获取值

转载 作者:行者123 更新时间:2023-11-29 12:23:15 24 4
gpt4 key购买 nike

我正在创建一个如下所示的 Plist 文件

enter image description here

我想列出级别为 1 的所有项目,如果级别为 1,我只能使用 accessoryType = UITableViewCellAccessoryCheckmark。我该怎么做。

我在这里加载我的 plist 文件:

- (void)viewDidLoad
{
[super viewDidLoad];
countId = 0;
NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Topic" ofType:@"plist"]];
self.items=[dict valueForKey:@"Items"];
self.itemsInTable=[[NSMutableArray alloc] init];
[self.itemsInTable addObjectsFromArray:self.items];

[self.menuTableView registerNib:[UINib nibWithNibName:NSStringFromClass([IndicatorTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([IndicatorTableViewCell class])];

UIBarButtonItem *myButton = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStylePlain
target:self
action:@selector(doneSelection:)];
[self.navigationItem setRightBarButtonItem:myButton];

}

我的 cellForRowAtIndexpath 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Name"];
return [self createCellWithTitle:Title image:[self.itemsInTable objectAtIndex:indexPath.row] indexPath:indexPath];
}

我的 didSelectRowAtIndexPath 代码是:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic=[self.itemsInTable objectAtIndex:indexPath.row];
if([dic valueForKey:@"SubItems"])
{
NSArray *arr=[dic valueForKey:@"SubItems"];

BOOL isTableExpanded=NO;

for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}

if(isTableExpanded)
{
[self CollapseRows:arr];
}
else
{
NSUInteger count=indexPath.row+1;
NSMutableArray *arrCells=[NSMutableArray array];
for(NSDictionary *dInner in arr )
{
[arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.itemsInTable insertObject:dInner atIndex:count++];
}
[self.menuTableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationLeft];
}
}

if([[[dic valueForKey:@"SubItems"]objectAtIndex:0]objectForKey:@"level"])
{
// NSArray *arr=[dic valueForKey:@"SubItems"];
// if ([[arr objectAtIndex:0 ] intValue] == @"1")
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryNone)
{
if(countId <5)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
countId = countId + 1;
}
else
{
UIAlertView *dialog;

dialog =[[UIAlertView alloc] initWithTitle:@"Alert Message"
message:@"Select maximum 5 countries"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
[dialog show];

// NSLog(@"Greater then 5");
}
}

else
{
if(countId>0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
countId--;
}
else
{
//show alert
UIAlertView *dialog;

dialog =[[UIAlertView alloc] initWithTitle:@"Alert Message"
message:@"Select atleast 1 country"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
[dialog show];

// NSLog(@"must choose 1");
}

}
// countId = [self.tableView indexPathsForSelectedRows].count;

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
}

请回复。我被困在这里

最佳答案

要检查 level = 1 添加 accessoryType = UITableViewCellAccessoryCheckmark try

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *itemDictionary = [self.itemsInTable objectAtIndex:indexPath.row];
NSArray *subItems = [itemDictionary valueForKey:@"SubItems"];
NSDictionary *firstItem = subItems[0];
if ([[firstItem objectForKey:@"level"] integerValue] == 1) {

//Set appropriate accessory view here
} else {

//Check the cell accessory type and update this too
//This is to avoid wrong accessory view on cell reuse
}

}

关于ios - 从 iOS 中的 plist 文件获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958805/

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