滚动UITableView时选定的行更改

时间:2015-03-26 02:53:39

标签: objective-c

目前我的情况是我的第一行默认选中。但是当我想取消选中它时,需要用户点击两次才能取消选中它。我可能知道我做错了吗?

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

    static NSString *cellIdentifier = @"Report_SelectGroupTableViewCell";
   Report_SelectGroupTableViewCell *cell = (Report_SelectGroupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Report_SelectGroupTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.indexPath = indexPath;
        cell.delegate = self;

    }
   // [myTableView selectRowAtIndexPath:0 animated:NO scrollPosition:UITableViewScrollPositionNone];
    //[self tableView:myTableView didSelectRowAtIndexPath:0];

    [cell setSelectionStyle:NO];


    NSString *groupID = [[groupArray objectAtIndex:indexPath.row] objectForKey:@"group_id"];

    if ([groupID isEqualToString:selectedGroup]){

        NSMutableDictionary *dict = [groupArray objectAtIndex:indexPath.row];
        BOOL isSelected = [[dict objectForKey:@"isSelected"] boolValue];
        if (!isSelected) {
            isSelected = !isSelected;
            [dict setObject:[NSNumber numberWithBool:isSelected] forKey:@"isSelected"];
        }
        cell.userInteractionEnabled = NO;
    }

    cell.groupName.text = [[groupArray objectAtIndex:indexPath.row] objectForKey:@"group_name"];

    NSMutableDictionary *typeDict = [groupArray objectAtIndex:indexPath.row];

    cell.tickButton.selected = [[typeDict objectForKey:@"isSelected"] boolValue];


    if (cell.tickButton.selected) {
        [cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
        if([cell.groupName.text isEqual:@"All Users"])
        {
            [cell.tickButton setImage:[UIImage imageNamed:@"tick_Icon_empty.png"] forState:UIControlStateNormal];

        }

    }
    else{
        [cell.tickButton.layer setBorderColor:[UIColor whiteColor].CGColor];
    }

    if([cell.groupName.text isEqual:@"All Users"])
    {

        [cell.tickButton setImage:[UIImage imageNamed:@"tick_Icon.png"] forState:UIControlStateNormal];
        [cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
        [typeDict setObject:@"1" forKey:@"isSelected"];


    }
      return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Report_SelectGroupTableViewCell *cell = (Report_SelectGroupTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    NSMutableDictionary *dict = [groupArray objectAtIndex:indexPath.row];

    BOOL isSelected = [[dict objectForKey:@"isSelected"] boolValue];
    isSelected = !isSelected;
    [dict setObject:[NSNumber numberWithBool:isSelected] forKey:@"isSelected"];



    if (cell.tickButton.isSelected == YES)
    {
        [cell.tickButton setSelected:NO];
        [cell.tickButton.layer setBorderColor:[UIColor whiteColor].CGColor];
        if([cell.groupName.text isEqual:@"All Users"])
        {
            [cell.tickButton setImage:[UIImage imageNamed:@"tick_Icon_empty.png"] forState:UIControlStateNormal];
        }

    }
    else if (cell.tickButton.isSelected == NO)
    {
        [cell.tickButton setSelected:YES];
        [cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];


    }

}

1 个答案:

答案 0 :(得分:0)

你应该删除

[myTableView selectRowAtIndexPath:0 animated:YES scrollPosition:
 UITableViewScrollPositionNone];

并使用它来选择第一行(这应该在此方法之外调用一次,或者您希望每次单元格渲染时都选择此行)

[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];

我不知道你为什么用这种方法称呼它

[myTableView.delegate tableView:myTableView didSelectRowAtIndexPath:0];

但是如果你想调用它,你也应该改为

[myTableView.delegate tableView:myTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

PS。您应该正确设置单元格的selectionStyle以查看单元格是否被选中。

更新:: 尝试使用此

替换方法中的所有代码
Report_SelectGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Report_SelectGroupTableViewCell"];
if (!cell){
    cell = [[[NSBundle mainBundle] loadNibNamed:@"Report_SelectGroupTableViewCell" owner:self options:nil] objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
return cell;

如果没有选择并突出显示第一行,请告诉我。