【问题标题】:shuffle NSMutableArray without repetition and showing in a UIButton随机播放 NSMutableArray 而不重复并在 UIButton 中显示
【发布时间】:2012-11-05 06:55:57
【问题描述】:

在我看来,我有 12 个按钮,一个数组包含 6 个名称,我想在 UIButton 标题中打印数组名称。这是我的代码:

texts = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];
UIButton *button;
NSString *name;
NSUInteger count = [texts count];
int i=0;

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
        button= (UIButton *)view;
        if(button.tag >= 1||button.tag <= 20)
        {
            int value = rand() % ([texts count] -1) ;
            int myTag= i+1;
            button = [self.view viewWithTag:myTag];
            name=[NSString stringWithFormat:@"%@",[texts objectAtIndex:value]];
            [button setTitle:name forState:UIControlStateNormal];
            NSLog(@"current  name :%@",name);
        }
        i++;
    }

}
[super viewDidLoad];

我面临的问题是:

  1. 在重复洗牌时,我尝试使用What's the Best Way to Shuffle an NSMutableArray?,但它不起作用。

  2. 我想要 12 个按钮中的 6 个标题,这意味着每个标题将位于 2 个按钮中。请帮我解决这个问题。我应该做出哪些改变?

【问题讨论】:

  • 1.您的数组中有 8 个标签,而不是 6 个。您从数组中随机选择一个标题,但没有从选项列表中删除该标题,因此您可以获得重复项。
  • @futureelite7 我应该做些什么改变来从选择列表中删除标题,
  • 你想做什么?我不是 100% 确定我理解你的问题。
  • 我想在 12 个按钮中显示 6 个标题(我更新任务),每个标题在 2 个按钮中,随机顺序 @futureelite7
  • @futureelite7 我认为如果您解释如何从数组中删除项目以及可能为什么需要这样做会很有帮助,我相信这将是一个公认的答案:)

标签: iphone ios uibutton nsmutablearray


【解决方案1】:

基本上,您需要将洗牌逻辑与将名称添加到按钮功能分开。所以先打乱数组,然后设置按钮的名称。

[super viewDidLoad]; //Always super call should be the first call in viewDidLoad
texts = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", @"1",@"2",@"3",@"4",@"5",@"6", nil];
//adding all 12 button titles to array, use your own logic to create this array with 12 elements if you have only 6 elements

NSUInteger count = [texts count];

for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        NSInteger nElements = count - i;
        NSInteger n = (arc4random() % nElements) + i;
        [texts exchangeObjectAtIndex:i withObjectAtIndex:n];
}

UIButton *button = nil;
NSString *name = nil;
int i = 0;

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
        button= (UIButton *)view;
        if(button.tag >= 1 && button.tag <= 20)
        {
            name = [NSString stringWithFormat:@"%@",[texts objectAtIndex:i]];
            //assuming that the above texts array count and number of buttons are the same, or else this could crash
            [button setTitle:name forState:UIControlStateNormal];
            NSLog(@"current  name :%@",name);
            i++;
        }
    }
}

【讨论】:

  • 让我澄清一下(我觉得有必要),在这个解决方案中,used 名称被移动到数组的开头,下一个名称将从unused 变体中挑选,这些变体是在used 名称位置之后。 futureelite7 的建议是删除 used 名称,不再使用它们以避免重复,这样在下一次迭代期间,数组中只有“未使用”的名称。
  • @A-Live,对不起,我没明白你的意思。这段代码要做的是,每次我们调用这个方法时简单地打乱数组,并将按钮名称设置为打乱后的值。从 fasilmoham 之前的问题来看,我猜这就是他想要的解决方案。在这里查看 cmets stackoverflow.com/questions/13206426/…
  • @ACB 显示错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTitle:forState:]: unrecognized selector sent to instance
  • @fasilmoham,文本数组中的项目数和按钮数与我的 cmets 中提到的相同吗?你能证实吗?还要检查它是一个按钮,而不是您尝试设置标题的任何其他视图。完全按原样使用我的代码,或者尝试使用您的代码并确认。
  • 复制时您可能错过了检查if([view isKindOfClass:[UIButton class]]),这非常重要。 @ACB 这个解释当然不适合你,因为我认为他很难理解算法本身,所以我添加了一些解释。
【解决方案2】:

创建 NSMutableArray 的类别

@implementation NSMutableArray (ArrayUtils)
- (void)shuffle{
static BOOL seeded = NO;
if(!seeded)
{
    seeded = YES;
    srandom(time(NULL));
}
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
@end

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 2011-11-10
    • 2012-10-23
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多