我在以下實施中遇到問題。我定義了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;
}
添加的cellForRowAtIndexPath方法。 – KKRocks
我剛纔包括在內。 – hotspring