简体   繁体   English

在分段 UITableView 中存储多个选择

[英]Storing Multiple Selections in sectioned UITableView

I have a list of people in a UITableView, sectioned by the first letter of the persons name.我有一个 UITableView 中的人员列表,按人员姓名的第一个字母划分。

I want this list to allow MULTIPLE selections (imagine 100 people, in a list, sectioned by name, with a check box next to each name).我希望这个列表允许多个选择(想象 100 个人,在一个列表中,按名称划分,每个名称旁边都有一个复选框)。

No problem with the UI - all sorted.用户界面没问题 - 全部排序。

Its how to STORE the selections?它如何存储选择?

I need way to store and retrieve the selections (simple BOOL values) from some sort of array, by using the indexPath to find them.我需要通过使用 indexPath 来从某种数组中存储和检索选择(简单的 BOOL 值)的方法来查找它们。

Before using sections, it was easy - just an NSMutableArray, and retrieve by indexPath.row.在使用部分之前,这很简单——只需一个 NSMutableArray,并通过 indexPath.row 检索。 But with sections I have to find by BOTH section AND row.但是对于部分,我必须通过部分和行找到。 Multidimensional arrays don't seem to go well with Objective C, though a multidimensional c array was suggested in one post somewhere. Multidimensional arrays don't seem to go well with Objective C, though a multidimensional c array was suggested in one post somewhere.

I could save to the database - but thats a lot of save and retrieves as you scroll up and down a table checking and unchecking tickboxes.... and I dont need the data to persist in the DB anyway - going to then create records with pointers to the selected records.我可以保存到数据库中-但是当您上下滚动表格检查和取消选中复选框时,会进行大量保存和检索....而且我不需要将数据保留在数据库中-然后使用创建记录指向所选记录的指针。

Surely I am not the first person to reach this dilemma.当然,我不是第一个遇到这种困境的人。

Any ideas?有任何想法吗?

I'd still be interested to hear ideas - but after trawling the interwebs for hours (until 3am,) and not coming up with a solution, I decided to use an NSDictionary: constructing the key from the indexPath row and section thus:我仍然很想听听想法 - 但是在互联网上拖网几个小时(直到凌晨 3 点)并且没有提出解决方案之后,我决定使用 NSDictionary: 从 indexPath 行和部分构造键:

 - (void) setSelected:(NSMutableDictionary*)dict forIndexPath:(NSIndexPath*)indexPath withValue:(BOOL)selected{

            NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", (int)indexPath.section, (int)indexPath.row];

            [dict setObject:[NSNumber numberWithBool:selected] forKey:cellIdentifier];

 }

- (BOOL) getSelected:(NSMutableDictionary*)dict forIndexPath:(NSIndexPath*)indexPath {

    NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", (int)indexPath.section, (int)indexPath.row];
    BOOL selected=[[dict objectForKey:cellIdentifier]  boolValue];

    return selected;

}
- (BOOL) toggleSelected:(NSMutableDictionary*)dict forIndexPath:(NSIndexPath*)indexPath {

    NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", (int)indexPath.section, (int)indexPath.row];

    BOOL selected=[[dict objectForKey:cellIdentifier]  boolValue];
    selected=!selected;

    [self setSelected:dict forIndexPath:indexPath withValue:selected];

    return selected;
}

You could use a mutable array to store persons or indexes of persons that are selected.您可以使用可变数组来存储所选人员或人员的索引。 This will go inside your didSelectRowAtIndexPath这将 go 在您的 didSelectRowAtIndexPath 中

UITableViewCell *currentCell = [self.tableView cellForRowAtIndexPath:indexPath];
if (currentCell.accessoryType == UITableViewCellAccessoryNone) {
    currentCell.accessoryType = UITableViewCellAccessoryCheckmark;
    //save/delete your person or index in/from a global mutable array
}

And in your cellForRowATIndexPath you could set the accessory based on whats in the array and whats not.在您的 cellForRowATIndexPath 中,您可以根据数组中的内容和不存在的内容来设置附件。 Hope it helps:)希望能帮助到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM