如何在iPhone的表格视图中保留索引路径值? [英] How to preserve the index path value in the table view in iPhone?

查看:58
本文介绍了如何在iPhone的表格视图中保留索引路径值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个表格视图并显示了数据.当我单击表视图中的数据时,我会打上辅助标记(使用UITableViewCellAccessoryCheckmark,Like,Check Mark).现在我想保留索引位置的状态.因为当我转到另一个班级然后返回表视图时,将显示先前的状态(附件标记).所以我该如何保存状态,这意味着存储或保存indexpath值.(不使用AppDelegate方法)那么我该如何实现呢?

I have created a table view and displayed the datas. When i click the data in the table view, i put the accessory mark (using UITableViewCellAccessoryCheckmark, Like, Check Mark). Now i want to preserve the state of index position. Because when i go to the another class and then come back to the table view, the previous state(accessory mark) will be displayed. SO how can i preserve the state, which means store or save the indexpath value.(Without using AppDelegate method)so How can i achieve this?

我的示例代码是

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 if (newRow != oldRow)
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                checkedData];
    oldCell.accessoryType = UITableViewCellAccessoryNone;

    checkedData = indexPath;

}
if (newRow == oldRow) {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {

        // cell.accessoryType = UITableViewCellAccessoryNone;
    }
    checkedData = indexPath;
}
 }

返回表视图类时,应保留以前的状态.那我该怎么访问呢?

When back to the table view class, the previous state should be preserved. so how can i access that?

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(checkedData == indexPath) // Doesn't works
      {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }

请帮帮我.

谢谢

推荐答案

一旦达到范围的末尾,变量将不可用(我不知道它们是nil还是发布,我只是知道您不能使用它们).

Variables are unavailable once you reach the end of scope (I don't know if they're nil or released, I just know you can't use them).

您想要做的是A)将值保存到持久性存储中 B)创建一个持久性变量.

What you want to do is A) save that value to persistent storage or B) make a persistent variable.

A)将对象设置为存储对象(在本例中为NSUserDefaults,因为它非常简单:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
prefs = [setObject:checkedData forKey:@"checkedData"];

然后,按照您想要的方式检查对象,您可以执行以下操作:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if([[prefs objectForKey:@"checkedData"] compare: indexPath]== NSOrderedSame){
cell.AccessoryType = UITableViewCellAccessoryCheckMark;
}

B)将对象另存为持久变量:

B) Save the object as a persistent variable:

在您的.h中:

@interface ... : ...{

}

@property(nonatomic, retain) NSIndexPath *checkedData;

@end

在.m中:

@implementation 
@synthesize checkedData;
@end

现在,要设置此变量:

self.checkedData = indexPath;

要检查它:

if([self.checkedData compare:indexPath] == NSOrderedSame){
cell.accessoryType = UITableViewCellAccessoryTypeCheckMark;
}

确实不会有很大的差异,这取决于您来决定您想要什么.但是,如果您使用NSUserDefaults,请记住,数据在每次启动时都会保留.如果您不想这样做,但要使用它,则必须在关闭应用程序时清空对象:[prefs removeObjectForKey:@"checkedData"];

There really isn't going to be a whole lot of difference, it's up to you to decide what you want. If you use NSUserDefaults though, keep in mind that the data persists across launches. If you don't want that, but you want to use it you have to empty the object when you close the app: [prefs removeObjectForKey:@"checkedData"];

快乐的编码,

赞恩

这篇关于如何在iPhone的表格视图中保留索引路径值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆