【问题标题】:random background image in UITableviewCellUITableviewCell 中的随机背景图像
【发布时间】:2013-12-15 09:23:40
【问题描述】:

我想知道如何构建具有随机背景图像的动态 UItable?例如,我有 6 张图片分别称为 back1.png、back2.png、back3.png......back6.png。

“cellForRowAtIndexPath”中要添加什么代码

我只能依次为每个单元格分配一个特定的图像,如下所示,

if(indexPath.row == 0){
    newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood1.jpg"]];
}
if(indexPath.row == 1){
    newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood2.jpg"]];
}
if(indexPath.row == 2){
    newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightwood3.jpg"]];
}

但是我如何才能在 UITableView 中随机更改为无限行?

【问题讨论】:

  • [NSString stringWithFormat:@"lightwood%d",arc4random()%6]

标签: iphone ipad ios7 uitableview


【解决方案1】:

试试这个...

int lowerBound = 1;
int upperBound = 7;
int randomValue = lowerBound + arc4random() % (upperBound - lowerBound);

newView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"lightwood%d.jpg", randomValue]]];

【讨论】:

  • 错误信息显示:方法调用的参数太多,预期1,有2。这是什么意思以及如何解决?谢谢
【解决方案2】:

试试这个,可能对你有帮助

newView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"lightwood%d.jpg", indexPath.row]]];

indexPath.row 一一增加,您的图像名称也一一增加.... 谢谢

【讨论】:

  • 在这种情况下它不起作用,因为只有 6 张图片,当 indexPath.row 返回大于 6 时,您的代码将加载图片。
  • 我有其他方法你需要使用 If else 条件来管理它
  • 如何使用 if else 条件管理随机函数?能详细点吗??
  • 看到我给了另一个答案希望这个工作正常...谢谢
猜你喜欢
  • 1970-01-01
  • 2013-01-14
  • 2013-06-21
  • 2012-02-08
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多