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 }