将选定的清单行保存在 NSuserdefaults 中
我对 iPhone 还很陌生,这是我项目中的最后一个模块。以下代码非常适合使用 chekmark 选择多个国家/地区。 基本上我的问题是如何保存这些选定的国家/地区并在返回此视图时再次显示复选标记。 请在这里帮助我..
我的 didSelectRowAtIndexPath 方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if([selectedCell accessoryType] == UITableViewCellAccessoryNone)
{
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
}
else
{
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[tableView reloadData];
}
我的 cellForRowAtIndexPath 方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate;
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[delegatObj.allcountryarray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
for (int i = 0; i < selectedIndexes.count; i++)
{
NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];
if (num == indexPath.row) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
break;
}
}
return cell;
}
谢谢
Iam pretty new to Iphone and this is my last module in my project..The following code working perfectly for multiple country selection with chekmark.
Basically my question is how to save those selected countries and show the check marks again when come back to this view.
please help me here..
My didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if([selectedCell accessoryType] == UITableViewCellAccessoryNone)
{
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
}
else
{
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[tableView reloadData];
}
My cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate;
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[delegatObj.allcountryarray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
for (int i = 0; i < selectedIndexes.count; i++)
{
NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];
if (num == indexPath.row) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
break;
}
}
return cell;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你的数据不是很大,你可以使用 NSUserDefaults。
保存数据:
获取数据:
If your data is not so big you can use NSUserDefaults.
To save data:
To get data:
有很多方法可以解决这个问题 -
根据我的说法,你必须创建一个与你的
Country array
相同的NSMutableArray
和array.cout
以及最初,您将默认值
存储在数组
中,在
didSelectRowAtIndexPath
中,您必须根据UITableViewCellAccessoryCheckmark
更改存储的默认值
。之后,您必须将
NSMutableArray
存储在NSUserDefaults
中 -链接 - 用于在 NSUserDefaults 中保存数组
There are so many way to solve it --
According to me you have to create a
NSMutableArray
andarray.cout
same as yourCountry array
and initially you store thedefault value
in yourarray
,in
didSelectRowAtIndexPath
you have to change the storeddefault value
according toUITableViewCellAccessoryCheckmark
.And after that you have to store the
NSMutableArray
in theNSUserDefaults
-Link - FOR SAVE ARRAY IN NSUserDefaults