先看看效果
然后选择其他房间
随便选中之后
看起来很简单,但实现却不是那么容易
首先自已要定义一个TableVIew,设置它的大小delegate之类的
这是viewDidload里面的
//TAbleView初始时的数据
NSArray *array=[[NSArrayalloc]initWithObjects:@"usa ",@" gucci",@"momo",@" lv",@"qq",@"6",@"7",@"8",@"牡丹",@"玫瑰",@"荷花",@"菊花",@"腊梅",@"夹竹桃",@"牵牛花",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",nil];
// 生成随机数
roomsNames = [[NSArrayalloc]init];
roomsNames =array;
NSMutableSet *randomSet1 =[[NSMutableSetalloc]init];
NSMutableSet *randomSet2 =[[NSMutableSetalloc]init];
NSMutableSet *randomSet3 =[[NSMutableSetalloc]init];
//5个控件显示
int x1 =arc4random()%10;
NSLog(@"%d",x1);
while ([randomSet1count]<x1)
{
int r1 =arc4random() % [roomsNamescount];
[randomSet1addObject:[roomsNamesobjectAtIndex:r1]];
}
randomArray1 = [randomSet1allObjects];
int x2 =arc4random()%10;
NSLog(@"%d",x2);
while ([randomSet2count]<x2)
{
int r2 =arc4random() % [roomsNamescount];
[randomSet2addObject:[roomsNamesobjectAtIndex:r2]];
}
randomArray2 = [randomSet2allObjects];
int x3 =arc4random()%10;
NSLog(@"%d",x3);
while ([randomSet3count]<x3)
{
int r3 =arc4random() % [roomsNamescount];
[randomSet3addObject:[roomsNamesobjectAtIndex:r3]];
}
randomArray3 = [randomSet3allObjects];
[_tableView reloadData];
}
if ([roomsNamescount]%7 ==0)
{
return [roomsNamescount]/7;
}
else
return [roomsNamescount]/7+1;
cell的填充内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *CellIdentifier =@"cellID";
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
//判断cell是否为空
if (cell ==nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
//cell的背景颜色
// NSInteger row = [indexPath row];
_tableView.separatorStyle =NO;
cell.selectionStyle =UITableViewCellSelectionStyleNone;
// 设置房间按钮
// 隐藏原有按钮
for (UIView *btnin cell.contentView.subviews) {
[btn removeFromSuperview];
}
int tag;
BOOL isLastRow = (indexPath.row +1) *7 >= roomsNames.count;
int roomNum =0;
// 不是最后一行
if (!isLastRow ||roomsNames.count %7 == 0) {
roomNum =7;
}else {
//是最后一行
roomNum =roomsNames.count %7;
}
for (int i=0; i<roomNum; i++) {
UIButton * row1Btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
row1Btn.frame =CGRectMake(i*120+50,20,96, 96);
tag = i + indexPath.row *7;
row1Btn.hidden =NO;
[row1BtnsetTag:tag];
[row1Btn addTarget:selfaction:@selector(TapRowBtn:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentViewaddSubview:row1Btn];
[row1Btn setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
//按钮按下的状态
[row1Btn setTitleColor:[UIColorblackColor]forState:UIControlStateHighlighted];
// ios7使用 [cell.contentView addSubview:row1Btn];
[row1Btn setTitle:[roomsNamesobjectAtIndex:tag]forState:UIControlStateNormal];
row1Btn.backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed:@"free"]];
}
//获得循环之后的数据
// NSLog(@"%@",[NSString stringWithFormat:@"%d",tag]);
return cell;
}
roomsNames.count %7的个数