0

我有一个列出货币的表格视图...我希望一次只能选择一个(如果用户单击已选择的单元格,它应该保持选中状态)...复选标记是一个 uiimageview...这里是代码:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.currencyList fetchedObjects] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CurrencyCell";

    CurrencyCell *cell = (CurrencyCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Currency *currency = [self.currencyList objectAtIndexPath:indexPath];

    cell.currencyName.text = currency.name;
    cell.curencyAbreviation.text = currency.abreviation;

    if ([currency.isDefault boolValue] == YES) {
        cell.selectedCurrency.image = [UIImage imageNamed:@"Checkmark.png"];
        checkedCell = indexPath;
        NSLog(@"The default currency cellfor row is :%@",currency);
        NSLog(@"the checkedcell index path is:%@",checkedCell);
    }

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // [self.delegate theSelectedCurrencyIs:[self.currencyList objectAtIndexPath:indexPath]];

    if (![indexPath isEqual:checkedCell]) {
    CurrencyCell *cell = (CurrencyCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    cell.selectedCurrency.image = [UIImage imageNamed:@"Checkmark.png"];
    selectedCurrency = [self.currencyList objectAtIndexPath:indexPath];
        NSLog(@"The selected currency is %@",selectedCurrency);
        NSLog(@"The indexpath of the new selected currency is %@",indexPath);
    CurrencyCell *previoslySelectedCell = (CurrencyCell *)[self.tableView cellForRowAtIndexPath:checkedCell];
        NSLog(@"name of the previously selected cell %@",previoslySelectedCell.currencyName);
    previoslySelectedCell.selectedCurrency.image = nil;
        //  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,checkedCell, nil] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView reloadData];
    checkedCell = indexPath;
    }
}

问题是复选标记图像显示在多个选定的单元格中..选定的索引路径没问题...但是很多单元格看起来都已选中...似乎每个滚动屏幕上都有一些

这里会有什么问题?

4

0 回答 0