2011-09-18 28 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