2011-09-18 43 views
0

我在计算nsmutablearray中的对象数时遇到了问题。我有这样的代码:查找NSMutableArray中的对象数

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{  

NSUInteger numObjects = [selected count];// selected is nsmutablearray 

for (int i = 0; i<numObjects; i++) 
{ 
    NSLog(@"%@ == %@ , nr.object =%@",[AnotherArray objectAtIndex:indexPath.row], [selektuara objectAtIndex:i], numObjects); // here stops the by reporting this: Program recived signal: "EXC_BAD_ACCESS" 


    if ([selected objectAtIndex:i] == [AnotherArray objectAtIndex:indexPath.row]) 
    { 
     NSLog(@"%@",[selected objectAtIndex:i]); 
     return UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     return UITableViewCellAccessoryNone; 
    } 
    } 
} 

当我删除NSLog对象的数量,它只计算一个对象,只比较它。 那么有其他解决方案如何获得这个数字? 谢谢。

回答

2

错误使用NSLog

NSLog(@"%@ == %@ , nr.object =%d",[AnotherArray objectAtIndex:indexPath.row], [selektuara objectAtIndex:i], numObjects); 

由于numObjectsNSUInteger你应该使用%d打印其价值。

错误的比较:

if ([[selected objectAtIndex:i] compare:[AnotherArray objectAtIndex:indexPath.row]] == NSOrderedSame) 

你不应该使用==在Objective-C的

+0

Thnx很多,现在我有这个数字,但我在循环返回时有另一个问题,即不让我比较其他对象,它只比较第一个对象,然后返回UITableViewCellAccessoryNone; – user751162

+0

所以当你找到平等时不要返回 – Nekto

1

你千万不要尝试输出与%@格式说明一个无符号整数比较的对象。改为使用%u

+0

你知道我该如何在这里“记住”选中的单元格吗?我将选中的单元格文本保存在“选定”对象中? – user751162