@property (strong,nonatomic)NSMutableSet*selectedRows;
@property(nonatomic,retain) IBOutlet UITableView *tableView;
...
self.selectedRows = [NSMutableSet new];
...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedRows containsObject:indexPath]){
[self.selectedRows removeObject:indexPath];
}else{
[self.selectedRows removeAllObjects];//test 1 item only
[self.selectedRows addObject:indexPath];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"heightForRowAtIndexPath %@ \nSelected %@", indexPath,self.selectedRows);
for (NSIndexPath*item in self.selectedRows) {
if ([item compare:indexPath]==NSOrderedSame) {
NSLog(@"EXIST1");
}
if (item.section == indexPath.section && item.row == indexPath.row) {
NSLog(@"EXIST2");
}
}
if ([self.selectedRows containsObject:indexPath] == YES) {
NSLog(@"EXIST3");
}
return 44.0f;
}
重绘细胞后,我希望筛选出选定的细胞。
问题是为什么我只在日志中得到这个: EXIST1 EXIST2
EXIST3在哪里?
简单测试:
NSMutableSet*s =[NSMutableSet new];
NSIndexPath*p1 = [NSIndexPath indexPathForRow:1 inSection:1];
NSIndexPath*p2 = [NSIndexPath indexPathForRow:1 inSection:1];
NSComparisonResult r1= [p1 compare:p2];
NSLog(@"p1 compare p2 = %@", r1==NSOrderedSame?@"YES":@"NO" );
[s addObject:p1];
NSLog(@"contain p1 =%@", [s containsObject:p1]==YES?@"YES":@"NO" );
NSLog(@"contain p2 =%@", [s containsObject:p2]==YES?@"YES":@"NO" );
NSLog(@"s = %@",s);
结果
2014-01-08 04:42:57.477 test[28342:70b] p1 compare p2 = YES
2014-01-08 04:42:57.478 test[28342:70b] contain p1 =YES
2014-01-08 04:42:57.478 test[28342:70b] contain p2 =YES
2014-01-08 04:42:57.479 test[28342:70b] s = {(
<NSIndexPath: 0x8b2e0d0> {length = 2, path = 1 - 1}
)}
答案 0 :(得分:1)
小NSIndexPath类别修复
@interface NSIndexPath (Extended)
-(NSUInteger)hash;
@end
@implementation NSIndexPath (Extended)
-(NSUInteger)hash{
char str[11];
int row = self.row;
int section = self.section;
sprintf(str, "%d%d", section,row);
unsigned int val = 0;
char *p;
int i;
p = str;
for(i = 0; p[ i ]; i++){
if (i ==0) {
val = (unsigned char)p[i] << CHAR_BIT;
}
else
val |= (unsigned char)p[i];
}
return val;
}
@end
答案 1 :(得分:0)
我有同样的问题,我通过过滤集合来解决它。我不确定哪个更有效率,但这足以达到我的目的。 代码来自对heightForRowAtIndexPath的调用
NSSet * set = [self.hiddenPaths filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"section == %u AND row == %u",indexPath.section, indexPath.row]];
if (![set count])
{
return 0.0f;//cell at indexPath is hidden
}