【问题标题】:Is there anyway to populate a UITableView with an array but persist checkmarks?无论如何用数组填充 UITableView 但保留复选标记?
【发布时间】:2010-02-15 04:07:45
【问题描述】:

我有一个带有标签栏的应用,每个标签都包含一个单独的表格。第一个表使用核心数据来保存其条目和复选标记。第二个选项卡上的第二个表使用 NSMutableArray 来填充它(我会使用核心数据,但我必须预先填充它,而这个表不允许这样做)我想以与第一个相同的方式保留复选标记包含核心数据的表,但有问题。代码如下所示:

 -(void)viewDidLoad {
    [super viewDidLoad];
airport = [[NSMutableArray alloc] init];
[airport addObject:@"Passport"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [airport count];
}


// Customize the appearance of table view cells.
- (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] autorelease];
}

// Set up the cell...
NSString *cellValue = [airport objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
//[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


NSManagedObject *item = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = [item valueForKey:@"name"]; //CHANGED TO DETAIL


if ([[item valueForKey:@"check"] boolValue]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}

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


NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];


if ([[selectedObject valueForKey:@"check"] boolValue]) {
    [selectedObject setValue:[NSNumber numberWithBool:NO] forKey:@"check"];
} else {
    [selectedObject setValue:[NSNumber numberWithBool:YES] forKey:@"check"];
}

UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; 
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
    thisCell.accessoryType = UITableViewCellAccessoryCheckmark;  
} else {
    thisCell.accessoryType = UITableViewCellAccessoryNone;
} 
[tableView deselectRowAtIndexPath:indexPath animated:NO];       

}

我相信这行 cell.textLabel.text = [item valueForKey@"name"]; 是什么原因造成的。我想要做的就是从数组中填充表格并保留复选标记。任何帮助是极大的赞赏。提前致谢。

【问题讨论】:

    标签: core-data uitableview persistence nsmutablearray


    【解决方案1】:

    这一行正在替换单元格文本:

    cell.textLabel.text = [item valueForKey:@"name"];
    

    最初由这些行分配:

    NSString *cellValue = [airport objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
    

    为什么要使用“item”的“name”属性?

    【讨论】:

    • 名称是名为“Hero”的实体中的一个属性 这就是我的第一个表的工作方式,它在加载时检查名称是否有复选标记。有什么方法可以保留一个带有数组或预加载列表的复选标记?
    • 有很多方法可以做你想做的事,包括使用 Core Data。我会使用 Core Data,因为您已经在使用它,但如果您愿意,您可以使用属性列表 (plist) 保存检查过的数据数组。
    • 如何使用 plist 来保留复选标记?数组会代替 plist 消失吗?
    • plist 是一种文件类型。您可以使用writeToFile:atomically: 将数组作为plist 写入存储(并使用initWithContentsOfFile: 读取它)。假设它是被标记为“已检查”的 Core Data 托管对象,您可能希望存储“已检查”对象的 objectID 属性数组(确保首先保存所有对象)。不过,为什么不使用 Core Data 呢?
    • 根据我对核心数据的经验,它不能预先填充。我曾经使用 Apple Developer Tech 支持核心数据,但被告知它不是。到目前为止,我只是想写信给 NSUserDefaults,因为这只是检查持久性。完整的代码可以在这里看到。我仍然不知道是什么让它这次崩溃了。 stackoverflow.com/questions/2267579/…我添加这个问题只是为了让人们可以看到更改后的代码。
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2014-02-08
    • 2014-10-17
    • 2023-03-16
    相关资源
    最近更新 更多