2017-07-15 92 views
0

我在以下實施中遇到問題。我定義了tableview中每個部分可以選擇多少個最大項目。可在每個部分中選擇項目的限制編號

假設允許用戶如果用戶嘗試選擇第三一個選擇在部分0只項目,那麼第一選擇的項目將被取消和第三個項目將有對號附件。

我的以下實現無法處理,它允許多於兩個項目與所有複選標記。我想知道我做錯了什麼?

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    int numberOfItemsSelected = [[selectedRowsInSectionDictionary valueForKey: [NSString stringWithFormat:@"%ld",indexPath.section]] intValue]; 

    if(((ComboItem*)comboItemsArray[indexPath.section]).itemNumberEachCombo == numberOfItemsSelected) 
    { 
     NSIndexPath *oldIndex = [self.comboTableView indexPathForSelectedRow]; 
     [self.comboTableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone; 
     [self.comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
     return nil; 
    } 
    else 
    { 
     return indexPath; 
    } 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    [self selectedItem:self.comboTableView]; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    [self selectedItem:self.comboTableView]; 
} 

- (void)selectedItem: (UITableView *)tableView { 
    selectedRowsInSectionDictionary = [NSMutableDictionary dictionary]; 
    NSArray <NSIndexPath*> *selectedIndexPaths = [tableView indexPathsForSelectedRows]; 
    for (NSIndexPath *indexpath in selectedIndexPaths) { 
     NSString *sectionKey = [NSString stringWithFormat:@"%ld",indexpath.section]; 
     NSInteger numberOfSelectedRows = [[selectedRowsInSectionDictionary objectForKey: sectionKey] integerValue]; 
     numberOfSelectedRows++; 
     [selectedRowsInSectionDictionary setObject:@(numberOfSelectedRows) forKey: sectionKey]; 
    } 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    cell.textLabel.text = ((ComboItem*)comboItemsArray[indexPath.section]).allComboItems[indexPath.row]; 
    return cell; 
} 
+0

添加的cellForRowAtIndexPath方法。 – KKRocks

+0

我剛纔包括在內。 – hotspring

回答

2

編輯:

實施willSelectRowAtIndexPath:

方法看起來不正確我(試圖在我的樣本項目後)。另外,indexPathForSelectedRow返回行選擇數組中的第一個索引路徑對象;因此總是返回第0個部分的第一個選定索引。

這是除去方法之後執行 - (void)selectedItem: (UITableView *)tableView和selectedRowsInSectionDictionary變量和添加新NSMutable字典變量用於維持所選索引路徑的陣列,selectedIndexPathsInSectionDictionary

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    selectedIndexPathsInSectionDictionary = [NSMutableDictionary dictionary]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     int numberOfItemsSelected = [[selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)] count]; 

     if(((ComboItem*)comboItemsArray[indexPath.section]).itemNumberEachCombo == numberOfItemsSelected) 
     { 
      NSIndexPath *oldIndex = [[selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)] firstObject]; 
      [tableView deselectRowAtIndexPath:oldIndex animated:NO]; 
      [self tableView:tableView didDeselectRowAtIndexPath:oldIndex]; 
     } 
     return indexPath; 
    } 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
     [self selectItemAtIndexPath:indexPath]; 
    } 

    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
     [self deselectItemAtIndexPath:indexPath]; 
    } 

    - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath { 
     NSMutableArray* array = [selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)]; 
     if(array){ 
      [array addObject:indexPath]; 
     } else { 
      array = [NSMutableArray array]; 
      [array addObject:indexPath]; 
      [selectedIndexPathsInSectionDictionary setObject:array forKey:@(indexPath.section)]; 
     } 
    } 

    - (void)deselectItemAtIndexPath:(NSIndexPath*)indexPath { 
     NSMutableArray* array = [selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)]; 
     [array removeObject:indexPath]; 
    } 

enter image description here

樣本項目的github link

+0

由於某些原因,每當第二部分項目達到最大數量時,它也會取消選擇第一部分的項目。真奇怪。任何想法? – hotspring

+0

是的,我知道了,indexPathForSelectedRow總是返回行選擇數組中的第一個索引路徑對象;因此總是返回第0個部分的第一個選擇的索引,使得邏輯有缺陷。更好的辦法是維護一個選定的索引路徑的字典,其中關鍵字是索引路徑的部分和數組,值爲 –

+0

您可以添加到您的答案嗎?我已經有了這個邏輯(跟蹤字典中每個部分的選定indexPath, - '(void)selectedItem:(UITableView *)tableView',但我將如何使用它? – hotspring