根据NSUserDefaults值设置行选择?

时间:2014-04-07 07:19:34

标签: ios objective-c uitableview nsindexpath

我目前正在制作一个玩家可以选择要玩的角色的视图。我有一个tableView,每行代表一个字符。每一行内部都有一个按钮,玩家可以点按该按钮来选择相应的角色。用户第一次加载时,只有一个字符可用(第一行),我在viewDidLoad中执行以下操作:

//If not selected character exists set it to the default character
if (![[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"]) {
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"Current Character"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
NSLog(@"Value: %d", [[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"]);

默认情况下,每行中的所有按钮都会隐藏,并在其后面显示锁定图像。所以我在cellForRowAtIndexPath方法中执行以下操作:

//CHECK FOR UNLOCKED ITEMS
switch (indexPath.row) {
    case 0: {
        //Always show since it will always be available
        cell.playerButton.hidden = NO;
        break;
    }
    case 1: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item2Unlocked"]) {
            cell.playerButton.hidden = NO;
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    case 2: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item3Unlocked"]) {
            cell.playerButton.hidden = NO;
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    case 3: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item4Unlocked"]) {
            cell.playerButton.hidden = NO;
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    default:
        break;
}

到目前为止,一切都很顺利。 我面临的问题是弄清楚如何将正确行中的按钮更新为选定状态。我已尝试执行以下操作,但最终更新时间超过了一键:

//CHECK FOR UNLOCKED ITEMS
switch (indexPath.row) {
    case 0: {
        cell.playerButton.hidden = NO;
        if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 0) {
            cell.playerButton.selected = YES;
        } else {
            cell.playerButton.selected = NO;
        }
        break;
    }
    case 1: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item1Unlocked"]) {
            cell.playerButton.hidden = NO;
            if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 1) {
                cell.playerButton.selected = YES;
            } else {
                cell.playerButton.selected = NO;
            }
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    case 2: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item2Unlocked"]) {
            cell.playerButton.hidden = NO;
            if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 2) {
                cell.playerButton.selected = YES;
            } else {
                cell.playerButton.selected = NO;
            }
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    case 3: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item3Unlocked"]) {
            cell.playerButton.hidden = NO;
            if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 3) {
                cell.playerButton.selected = YES;
            } else {
                cell.playerButton.selected = NO;
            }
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    default:
        break;
}

这里有什么问题?

更新

执行以下操作后,所选状态最终有效:

//CHECK FOR UNLOCKED ITEMS
switch (indexPath.row) {
    case 0: {
        cell.playerButton.hidden = NO;
        if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 0) {
            cell.playerButton.selected = YES;
            NSLog(@"Character is selected.");
        } else {
            cell.playerButton.selected = NO;
        }
        break;
    }
    case 1: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item1Unlocked"]) {
            cell.playerButton.hidden = NO;
            if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 1) {
                cell.playerButton.selected = YES;
                NSLog(@"Character B is selected.");
            } else {
                cell.playerButton.selected = NO;
            }
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    case 2: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item3Unlocked"]) {
            cell.playerButton.hidden = NO;
            if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 2) {
                cell.playerButton.selected = YES;
                NSLog(@"character C is selected.");
            } else {
                cell.playerButton.selected = NO;
            }
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    case 3: {
        if([[NSUserDefaults standardUserDefaults] boolForKey:@"item4Unlocked"]) {
            cell.playerButton.hidden = NO;
            if ([[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"] == 3) {
                cell.playerButton.selected = YES;
                NSLog(@"Character D is selected.");
            } else {
                cell.playerButton.selected = NO;
            }
        } else {
            cell.playerButton.hidden = YES;
        }
        break;
    }
    default:
        break;
}

但是,如果选择按钮,如何禁用按钮或用户交互?

1 个答案:

答案 0 :(得分:1)

这应该做同样的事情并且更简单

//CHECK FOR UNLOCKED ITEMS
   int currentCharacter=[[NSUserDefaults standardUserDefaults] integerForKey:@"Current Character"];
   BOOL unlocked=NO;

   if (indexPath.row == 0)
   {
       unlocked=YES;        // Character 0 is always unlocked
   }
   else {
     unlocked=[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:"%@item%dUnlocked",indexPath.row]]
   }

   if (unlocked)
   {
      cell.playerButton.hidden=NO;
      if (indexPath.row == currentCharacter)
      {
          cell.playerButton.selected=YES;
          cell.playerButton.enabled=NO;      //Cannot select if already selected
      }
      else
      {
          cell.playerButton.selected=NO;
          cell.playerButton.enabled=YES;
      }
   }
   else
   {
      cell.playerButton.hidden=YES;
   }