2011-07-19 30 views
0

我有一個UITableView中的人員列表,由人名的第一個字母分段。在部分UITableView中存儲多個選擇

我想讓這個列表允許多選(想象100個人,在列表中,按名稱分區,每個名稱旁邊有一個複選框)。

UI沒有問題 - 全部排序。

它如何存儲選擇?

我需要通過使用indexPath來查找它們來存儲和檢索某些數組中的選擇(簡單布爾值)的方法。

使用節之前,很容易 - 只是一個NSMutableArray,並通過indexPath.row進行檢索。但有部分,我必須通過BOTH部分和行來找到。多維數組似乎不適合Objective C,儘管在某篇文章中建議使用多維c數組。

我可以保存到數據庫 - 但多數保存和檢索當你上下滾動一個表檢查和取消選中tickboxes ....我不需要數據堅持在數據庫中 - 無論如何然後用指向所選記錄的指針創建記錄。

當然,我不是第一個達成這個困境的人。

任何想法?

+0

你不能添加一個選定的屬性到你的人(或任何)的對象,從中建立細胞?然後,當您使用Person構建單元格時,只需添加或不添加複選框。或者你需要別的東西嗎? – SVD

+0

正如我在帖子中所說的,我不想將選定的屬性存儲在覈心數據中。 – fezfox

回答

0

我仍然有興趣聽到想法 - 但是在拖曳網頁數小時(直到凌晨3點!)而沒有提出解決方案之後,我決定使用NSDictionary,從indexPath行和節構建密鑰因此:

- (void) setSelected:(NSMutableDictionary*)dict forIndexPath:(NSIndexPath*)indexPath withValue:(BOOL)selected{ 

      NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", (int)indexPath.section, (int)indexPath.row]; 

      [dict setObject:[NSNumber numberWithBool:selected] forKey:cellIdentifier]; 

} 

- (BOOL) getSelected:(NSMutableDictionary*)dict forIndexPath:(NSIndexPath*)indexPath { 

    NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", (int)indexPath.section, (int)indexPath.row]; 
    BOOL selected=[[dict objectForKey:cellIdentifier] boolValue]; 

    return selected; 

} 
- (BOOL) toggleSelected:(NSMutableDictionary*)dict forIndexPath:(NSIndexPath*)indexPath { 

    NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", (int)indexPath.section, (int)indexPath.row]; 

    BOOL selected=[[dict objectForKey:cellIdentifier] boolValue]; 
    selected=!selected; 

    [self setSelected:dict forIndexPath:indexPath withValue:selected]; 

    return selected; 
} 
0

您可以使用可變數組來存儲所選人員或人員的索引。這將大大的didSelectRowAtIndexPath方法

UITableViewCell *currentCell = [self.tableView cellForRowAtIndexPath:indexPath]; 
if (currentCell.accessoryType == UITableViewCellAccessoryNone) { 
    currentCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    //save/delete your person or index in/from a global mutable array 
} 

內,在你的cellForRowAtIndexPath你可以基於什麼數組中,並沒有什麼設置的附件。希望它有幫助:)

+0

Xs2Bush謝謝 - 但是你用什麼樣的索引來將BOOL(或indexPath)存儲在一個可變數組中,這將允許你在cellForRowAtIndexPath中檢索它?我找不到任何方法將indexPath轉換爲可變數組的索引....如果您知道如何,請發帖! (你是對的 - 我可以將整個對象存儲在一個數組中 - 但是如果你使用一個數組作爲你的數據,你可以初始化一個相同大小的可變數組,並設置所有的數據,但是我想要的只是一個BOOL) – fezfox

+0

假。然後選擇一個單元格後,使用類似[selectionArray setObject:[NSNumber numberWithBool:YES] atIndex:indexPath.row]將該索引處的對象設置爲true。然後在indexpath的行中的單元格中檢查[[selectionArray objectAtIndex:indexPath.row] boolValue]的值是否爲true。 –

+0

這隻適用於只有一節的表格。但是這個表格有多個部分 - 所以你需要通過BOTH行和部分索引數據。數據也來自核心數據FRC。再次,我認爲我在最初的帖子中指出了這一點。 – fezfox