2014-01-19 39 views
0

需要關於for循環的幫助,我還沒有完全弄清楚。過了很長時間,我想我應該轉向專家。For Loop將存儲行的附件圖標存儲到陣列

我需要這個遍歷每個索引路徑並檢查附件圖標。如果它是複選標記,請將其添加到數組中,最後記錄數組。

這一切都與一個按鈕操作綁定。這裏是代碼:

//Handles tableView row selection and addition and removal of the checkmark 
- (void)tableView:(UITableView *)theTableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO]; 
    UITableViewCell *cell = [theTableView cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     //Reflect selection in data model 
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     //Reflect deselection in data model 
    } 
} 

    //Sends checkmarked items to console log 
    - (IBAction)sendResults:(id)sender { 
     NSMutableArray *aList = [[NSMutableArray alloc] init]; 

    //This is the part that isn't quite right 
    for (NSIndexPath *indexPath in _tableView.indexPathsForSelectedRows) { 
    NSString *r = [NSString stringWithFormat:@"%li",(long)indexPath.row]; 
    [aList addObject:r]; 
} 

     NSLog(@"Selected Items: %@", _aList); 
} 

我認爲問題是沒有我的表格視圖單元格被選中。在我的didSelectRowAtIndexPath方法中,我將附件圖標檢查爲複選標記,但似乎取消選中該行。所以當我登錄時我在_tableView.indexPathsForSelectedRows沒有任何對象(我在日誌中得到「Selected Items:(null)」。

這個問題很有意義,但我似乎無法解決這個問題。 ?謝謝!

+0

您在檢查附件類型之前取消選擇該行 – Joshua

回答

0

tableView:didSelectRowAtIndexPath:因爲你沒有選擇該行,所以當你的循環在選定的行(_tableView.indexPathsForSelectedRows)上執行時,沒有條目,在行旁邊顯示一個複選標記與使UITableView實際識別不同它被選中

要解決此問題或者刪除[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];或更改您的循環以測試cell.accessoryType = UITableViewCellAccessoryCheckmark