0

我正在尝试使用 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;
}
4

2 回答 2

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;
}
于 2013-04-04T09:31:30.577 回答
0

这里的cellone是tableviewCell,在那个单元格中你可以添加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;      
   }           

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

于 2013-04-04T09:44:58.200 回答