简体   繁体   English

多单元搜索选择

[英]Multiple cell selection with search

I've created a UITableView which contains a list of teams which all have a accessoryView. 我创建了一个UITableView ,其中包含一个团队列表,每个团队都有一个附件视图。 This accessoryView indicates whether a cell is selected or not. 这个AccessoriesView指示是否选择了一个单元格。 Each cell is connected with a custom Object in my Team class. 每个单元格都与我的Team类中的自定义对象相连。

When a cell is selected it then saves the particular team Object in a teamSelected array. 选择单元格后,它将特定的组对象保存在teamSelected数组中。

All this works fine. 所有这些都很好。 However there seem to be a issue when search and then filter the data and select a cell it seem to add the wrong object and change the accessoryView on a wrong object to? 但是,在搜索然后过滤数据并选择一个单元格时似乎出现了问题,它似乎添加了错误的对象,并将错误的附件视图更改为?

How come it does to not add the correct object on cell selection when the searchBar is active? 当searchBar处于活动状态时,为什么没有在单元格选择上添加正确的对象呢?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("teamCell", forIndexPath: indexPath) as! TeamCell

    if (self.teamSearchController.active) {
        cell.textLabel?.text = filteredTableData[indexPath.row].name

    } else {
        cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20)
        cell.textLabel?.text = self.teamArray[indexPath.row].name as String
    }

    let team = self.teamArray[indexPath.row] as Team
    var removed = false

    for (index, value) in enumerate(self.teamSelected) {
        if (value.id == team.id) {

            cell.accessoryView = cell.accessoryCheck
            removed = true
        }
    }

    if (!removed) {
        cell.accessoryView = cell.accessoryUncheck
    }

    cell.selectionStyle = UITableViewCellSelectionStyle.None

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if (self.teamSearchController.active) {
        team = self.filteredTableData[indexPath.row] as! Team
        removed = false
    } else {
        team = self.teamArray[indexPath.row] as Team
        removed = false
    }

    for (index, value) in enumerate(self.teamSelected) {
        if (value.id == team.id) {
            self.teamSelected.removeAtIndex(index)
            removed = true
        }
    }

    if (!removed) {
        self.teamSelected.append(team)
    }

    var userDefaults = NSUserDefaults.standardUserDefaults()
    let encodedData = NSKeyedArchiver.archivedDataWithRootObject(self.teamSelected)
    userDefaults.setObject(encodedData, forKey: "teams")
    userDefaults.synchronize()

    tableView.reloadData()
}

I have done this in objective c. 我已经在目标c中做到了这一点。 I have also used uisearchview Controller. 我还使用了uisearchview控制器。 I have use this code. 我已使用此代码。 cellSelected is a nsmutable array. cellSelected是一个nsmutable数组。 Use this code in didSelect method of table View. 在表View的didSelect方法中使用此代码。

if (tableView == self.searchDisplayController.searchResultsTableView) { 如果(tableView == self.searchDisplayController.searchResultsTableView){

       if ([self.cellSelected containsObject:[self.searchArray objectAtIndex:indexPath.row][@"id"]])
       {
           [self.cellSelected removeObject:[self.searchArray objectAtIndex:indexPath.row][@"id"]];


       }
       else
       {
        [self.cellSelected addObject:[self.searchArray objectAtIndex:indexPath.row][@"id"]];

               [self.selectedCategoryArray addObject:getSelectedCategory];   
       }
   } 

after this use this code in cellforrowatindexpath method 之后,在cellforrowatindexpath方法中使用此代码

if (tableView == self.searchDisplayController.searchResultsTableView)
   {
       NSLog(@"self.searchArray..%@",self.searchArray);
       titleName.text = [self.searchArray objectAtIndex:indexPath.row][@"name"];
       if ([self.cellSelected containsObject:[self.searchArray objectAtIndex:indexPath.row][@"id"]])
       {
           cell.accessoryType = UITableViewCellAccessoryCheckmark;
       }
       else
       {
           cell.accessoryType = UITableViewCellAccessoryNone;
       }
   } 

In the above code make else and use your actual array. 在上面的代码中,执行else并使用您的实际数组。 hope this will help you. 希望这会帮助你。 Thanks 谢谢

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

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