UITableviewCell中的随机背景图像

时间:2013-12-15 09:23:41

标签: iphone ipad ios7 uitableview

我想知道如何构建具有随机背景图像的动态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中随意改变它无限行?

2 个答案:

答案 0 :(得分: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 :(得分:0)

试试这个,可能会有所帮助

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

indexPath.row逐个增加,你的图片名称也逐一增加.... 谢谢