【发布时间】:2014-12-29 06:25:52
【问题描述】:
我是 iOS 开发的新手。我想制作一个包含 UITableview 的应用程序。我想在用户选择 UITableView 单元格时显示,然后它就像检查标记的那样按我想要的方式工作,但现在我想在用户返回其他视图并返回表视图时显示,然后它会显示您过去选择的表视图单元格给我解决方案。
我在 Stack Overflow 中显示它是由 NSUSerdefault 完成的
这里是我的 UITableview didSelectRowAtIndexPath 方法。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCustumCell *cell = (CategoryCustumCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *selectedCell= cell.categoryLabel.text;
NSLog(@"Category label %@",selectedCell);
NSString *text=[self.categoryArray objectAtIndex:indexPath.section];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath])
{
[self.selectedCells removeObject:indexPath];
[self.selecedStates removeObject:text];
cell.accessoryType = UITableViewCellAccessoryNone;
} else
{
[self.selectedCells addObject:indexPath];
[self.selecedStates addObject:text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(@"%@", self.selecedStates);
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:[NSString stringWithFormat:@"%d",indexPath.section] forKey:@"lastIndexPathUsed"];
[userdefaults synchronize];
}
还有 UITableView 的代码cellForRowAtIndexPath
-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
我在 viewDidLoad 中写入 Fetch NSUserdefault Data,就像
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.lastIndexPath = [defaults objectForKey:@"lastIndexPathUsed"];
我在我的.h 文件中定义
@property (nonatomic,retain) NSIndexPath *lastIndexPath;
但我没有得到用户选择返回视图请给我解决方案。
【问题讨论】:
-
你能显示你的 cellForRowAtIndexPath 方法的代码吗?您需要使用 NSUSerDefaults 生成单元格视图。您还在使用 indexPath.section 而不是 indexPath.row 吗?您的表格视图中有多个部分吗?
-
@NewStackUser 我在这里写 cellForRowAtIndexPath 代码,在这里我的 Tableview 多个部分。
-
@iOSDeveloper- 查看我的答案,您可以用您的 nsuserdefault 值替换我的属性。
-
@All 感谢您帮助我拯救我的一天。
标签: ios objective-c iphone uitableview