我创建了一个UITableView
,其中包含所有拥有附件视图的团队列表。此附件视图指示是否选择了单元格。每个单元格都与我的Team
类中的自定义对象连接。
选择单元格后,它会将特定团队对象保存在teamSelected数组中。
这一切都很好。但是,搜索然后过滤数据并选择一个单元似乎添加了错误的对象并更改了错误对象上的accessoryView时,似乎存在问题?
当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()
}
答案 0 :(得分:0)
我在目标c中做到了这一点。我也使用了uisearchview控制器。我使用过这段代码。 cellSelected是一个nsmutable数组。在表View的didSelect方法中使用此代码。
if(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];
}
}
之后在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;
}
}
在上面的代码中make else并使用你的实际数组。希望这会帮助你。感谢