tableView上的按钮功能

先看看效果


然后选择其他房间


随便选中之后


看起来很简单,但实现却不是那么容易

首先自已要定义一个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];

    

}

其中最重要的是  [ _tableView   reloadData ];
如果不重新加载数据,那上面就是空白的
点击按钮
按钮tag是多少,randomArray就是多少
这样是因为每个房间的随机数不相同,所以我生成了三遍的随机数
返回行数:我一行有7个按钮

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的个数



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值