先看看效果
然後選擇其他房間
隨便選中之後
看起來很簡單,但實現卻不是那麼容易
首先自已要定義一個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的個數