我有UITableView
,大约有20行。我还使用复选标记附件来识别所选的。我现在面临的问题是当我滚动时(这里我选择多行),选择变得混乱。所以在滚动后,所选的checkmark
已经消失了。任何人都可以帮我找到方法吗?在didSelectRowAtIndexPath:
NSMutableDictionary *rowDict = [tableList objectAtIndex:[indexPath row]];
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
else
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
checkedIndexPath = indexPath;
NSLog(@"the checked psths are :%@",checkedIndexPath);
}
在cellforrowatIndexpath
我使用
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
但这仅适用于单一选择。我应该怎样做多选?
答案 0 :(得分:2)
您可以拥有一个NSMutableArray
,可以添加n
个与所选单元格对应的NSIndexPath
个。取消选择单元格后,可以从数组中删除该索引路径。
答案 1 :(得分:1)
您应该使用数组来存储已检查的索引。如果您使用单个属性,则只有最后选择的行才能获得正确的accessoryType。
答案 2 :(得分:0)
我多次使用此代码(根据需要进行一些小的更改)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView
accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}