UITableViewCell在滚动时更改附件视图更改

时间:2013-04-04 09:27:03

标签: ios uitableview

我正在尝试使用UISwitch为某些特定行创建tableview,并为其余单元创建AccessoryDisclosureIndicator。

我得到了所需的结果作为下面代码中的一个检查。但问题是,当我突然滚动表格视图然后开关的位置改变..这是不希望的,我怎么能摆脱这个。

请回复此主题

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
 {
     int _fontSize = fontSize;
    NSArray *switchSettings = [[NSUserDefaults standardUserDefaults] arrayForKey:@"switchGeneralSettings"];

    NSLog(@"switch settings :%@",switchSettings);

    static NSString *cellIdentifier = @"generalSettingsTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSLog(@"*******************1*************");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        // code to additionally configure cell for multiple other UI components..


        if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
        {
            NSLog(@"switch*******************%d*************",indexPath.row);
              switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
            switchObj.tag = indexPath.row + 20002;
             cell.accessoryView = switchObj;
            [switchObj addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
            [switchObj release];

        }
        else
        {
            NSLog(@"indicator*******************%d*************",indexPath.row);

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }


    }
    else
    {

        if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
        {
             NSLog(@"*******************2*************");
            countSwitch = 0;
            switchObj = (UISwitch *)[cell.contentView viewWithTag:indexPath.row +  20002];
            // switch
            [switchObj setOn:NO];
            if ([switchSettings count]>0) {

                NSString *settingState = [switchSettings objectAtIndex:countSwitch];
                if ([settingState isEqualToString:@"ON"]) {
                    [switchObj setOn:YES];
                }

            }
            countSwitch++;


        }

    }
       // confiure the cell here...
    NSLog(@"*******************3*************");



    NSString *cellTextLabel = [listGeneralSettings  objectAtIndex:indexPath.row];

    cell.textLabel.text = cellTextLabel;
    cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];

    cell.detailTextLabel.text = [listDetailGeneralSettings objectAtIndex:indexPath.row];
    NSLog(@" cell.detailTextLabel.text :%@", cell.detailTextLabel.text);


    NSLog(@"*******************end*************");

    return cell;
}

2 个答案:

答案 0 :(得分:0)

您只是在第一次创建时更新单元格。您需要每次都更新它。试试这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     {         int _fontSize = fontSize;         NSArray * switchSettings = [[NSUserDefaults standardUserDefaults] arrayForKey:@“switchGeneralSettings”];

    NSLog(@"switch settings :%@",switchSettings);

    static NSString *cellIdentifier = @"generalSettingsTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSLog(@"*******************1*************");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    // code to additionally configure cell for multiple other UI components..
    // configure the cell here...

    if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
    {
        NSLog(@"switch*******************%d*************",indexPath.row);
        switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
        switchObj.tag = indexPath.row + 20002;
        cell.accessoryView = switchObj;
        [switchObj addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
        [switchObj release];

    }
    else
    {
        NSLog(@"indicator*******************%d*************",indexPath.row);

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
    {
        NSLog(@"*******************2*************");
        countSwitch = 0;
        switchObj = (UISwitch *)[cell.contentView viewWithTag:indexPath.row +  20002];
        // switch
        [switchObj setOn:NO];
        if ([switchSettings count]>0) {

            NSString *settingState = [switchSettings objectAtIndex:countSwitch];
            if ([settingState isEqualToString:@"ON"]) {
                [switchObj setOn:YES];
            }

        }
        countSwitch++;


    }

    NSLog(@"*******************3*************");



    NSString *cellTextLabel = [listGeneralSettings  objectAtIndex:indexPath.row];

    cell.textLabel.text = cellTextLabel;
    cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];

    cell.detailTextLabel.text = [listDetailGeneralSettings objectAtIndex:indexPath.row];
    NSLog(@" cell.detailTextLabel.text :%@", cell.detailTextLabel.text);


    NSLog(@"*******************end*************");

    return cell;
}

答案 1 :(得分:0)

此处cellone是表格视图单元格,在该单元格中可以添加uiswitch

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

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];    

    // ... some text label stuff deleted here.

    cell.opaque = NO;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    [cell setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"row_2_blank.png"]]];
    // [cell prepareForReuse];
}
if (indexPath.row == 0)
{
    selectAllB.tag=100;
    [selectAllB addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
    [cellOne setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"row_2_blank.png"]]];


    cellOne.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cellOne;
}
return cell;      
   }           

同样,您可以使用许多tableview单元格,您可以在其中相应地添加ui元素