0

我有一个 UITableView,它在选择一行时显示复选标记。问题是当表格视图滚动时,当只选择一个行时会显示多个复选标记。当表格滚动和出队过程发生时,问题就出现了。这是我的 cellForRowAtIndexPath: 方法

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

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];

        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];

        cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:56.0/255.0 blue:55.0/255.0 alpha:1];

    }

    // Get item from tableData
    NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];

    cell.textLabel.text = [item objectForKey:@"key"];

    [cell.textLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];

    return cell;
}

和 didSelect 方法:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    selectedCityTableString = cell.textLabel.text;

    cellAccessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-tick.png"]] ;
    cellAccessoryNoneImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]] ;

    if (cell.accessoryType==UITableViewCellAccessoryNone)
    {
        //        cell.accessoryType=UITableViewCellAccessoryCheckmark;

        cell.accessoryView = cellAccessoryImageView;

        if (prev!=indexPath.row) {
            cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prev inSection:0]];
            //cell.accessoryType=UITableViewCellAccessoryNone;
            cell.accessoryView = cellAccessoryNoneImageView;

            prev=indexPath.row;
        }
    }
    else{
        //        cell.accessoryType=UITableViewCellAccessoryNone;
        cell.accessoryView = cellAccessoryNoneImageView;

    }

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *mutCityStr = [[selectedCityTableString stringByReplacingOccurrencesOfString:@" " withString:@"+"] lowercaseString];

   // NSString *mutCityStr = @"c";

    [prefs setObject:mutCityStr forKey:@"selectedCityTableString"];
    [prefs synchronize];

#ifdef DEBUG    
    NSLog(@"mutCityStr is %@",mutCityStr);
    NSLog(@"selectedCityTableString is %@",selectedCityTableString);
    NSLog(@"posting notification from TWO TABLES");
#endif

    [[NSNotificationCenter defaultCenter] postNotificationName:@"TTSelectedList" object:selectedCountryTableString];

}
4

4 回答 4

3

假设您有一个名为的属性(属性),selectedRow请使用以下方法的组合:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...

    if (indexPath.row == self.selectedRow) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // If there is a cell selected deselect
    if (self.selectedRow != NSNotFound) {
        NSIndexPath *previousIndexPath = [NSIndexPath indexPathForRow:self.selectedAQIType inSection:0];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:previousIndexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Select the touched cell
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.selectedRow = indexPath.row;
}
于 2013-03-12T11:15:09.470 回答
2

In your .h file

int selectedRow;

In your .m file

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
        // your other code for cell init,etc
        int row = [indexPath row];
        cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

        cell.textLabel.textColor= (row == selectedRow) ? [UIColor colorWithRed:242.0f/255.0f green:104.0f/255.0f blue:42.0f/255.0f alpha:1] : [UIColor blackColor] ;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    selectedRow = [indexPath row];
    [tableView reloadData];
}

Hope this helps!!!

于 2013-03-12T11:24:26.547 回答
0

UITableView的委托方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath基于重用标识符重用单元格。因此,您需要将整个单元格内容重置为默认值。

在您的情况下,在返回单元格之前取消选中单元格的视图。

于 2013-03-12T11:10:42.253 回答
0

要解决此问题,请将您选择的行索引存储在一个NSArray. (在didSelectRowAtIndexPath方法中)

cellForRowAtIndexPath方法中,检查indexPath.row该数组中是否可用,然后启用您的复选标记,否则禁用。

注意:为检查取消检查事件维护数组

希望这会帮助你。

于 2013-03-12T11:14:27.080 回答