IOS列表实现动态多列

1 //图片列表
2 NSMutableArray *pictureList;
3 //分组列表
4 NSMutableArray *indexArr;

 

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     static NSString *picWallItemView = @"picWallItemView";
 4     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:picWallItemView];
 5     if (cell == nil) {
 6         cell = [[[UITableViewCell alloc]
 7                  initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [self tableView:self.tableView heightForRowAtIndexPath:indexPath])] autorelease];
 8     }
 9     int num = [[indexArr objectAtIndex:indexPath.row] intValue];
10     int index = 0;
11     for (int i = 0; i < indexPath.row; i++) {
12         index += [[indexArr objectAtIndex:i] intValue];
13     }
14     
15     float tmpWidth = self.view.frame.size.width / num;
16     float tmpHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
17     float left = 0;
18     for (int i = 0; i < num; i ++) {
19         left = i * tmpWidth;
20         PicWallItemView *itemView = [[PicWallItemView alloc] initWithFrame:CGRectMake(left, 0, tmpWidth, tmpHeight)];
21         itemView.delegate = self;
22         NSDictionary *jsonDic = [pictureList objectAtIndex:index];
23         PicWallItemTo *item = [PicWallItemTo fromDictionary:jsonDic];
24         [itemView setIndentationWidth:5];
25         [itemView reloadData:item];
26         [cell addSubview:itemView];
27         [itemView setFrame:CGRectMake(left, 0, tmpWidth, tmpHeight)];
28         [itemView release];
29         index++;
30     }
31     return cell;
32 }

在接收数据的地方,对数据进行分组。

 1         NSArray *arr = [obj objectForKey:MSG_DETALIST];
 2         
 3         NSUInteger total = [arr count];
 4         while (total > 0) {
 5             int num = arc4random() % 3 + 1;
 6             if(total > num){
 7                 [indexArr addObject:[NSNumber numberWithInt:num]];
 8                 total -= num;
 9             } else {
10                 [indexArr addObject:[NSNumber numberWithInt:total]];
11                 total = 0;
12             }
13         }    

 

  

转载于:https://www.cnblogs.com/warrior/p/3296038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值