如何显示UITableView中的多个列?

我想在UITableView中显示多个列。

例如:

的TableView

FName LName Age ----- ----- --- Abby Michale 34 

您可以在IB中定义一个自定义单元格,它将包含3个标签,并在函数中:

  - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *DetailCellIdentifier = @"DetailCell"; UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:DetailCellIdentifier]; if (cell == nil) { NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil]; cell = (UITableViewCell*) [cellObjects objectAtIndex:0]; } // setup your cell } 

当你在IB中定义单元格的时候,给每个标签一个标签,所以当你需要在那里放置一个文本时,你可以通过标签来检索它:

  label = (UILabel *)[cell viewWithTag:NAME_TAG]; label.text = myObject.name; 

并用其他2个标签重复此操作。 标签是一个唯一的编号。

把这个代码,而不是/设置您的单元格评论

我认为这样做的正确方法是UICollectionView。 从UITableView开始,最终因为在尝试实现左右和上下滚动时出现意外的行为而失败。

请检查这个真棒post一个完整的和最新的解决scheme: http : //www.brightec.co.uk/blog/uicollectionview-using-horizo​​ntal-and-vertical-scrolling-sticky-rows-and-columns

结果会是这样的:

在这里输入图像描述

我已经创build了UIGridView 。 我相信你的问题可以用同样的技术来解决。

你可以学习UIGridView的源代码。 代码真的很短。

创build一个包含三个UILabel元素作为子视图的自定义UIView子类。 然后将单元格的contentView属性设置为这个自定义视图。

将三个标签作为子视图添加到可用视图单元格的内容中,然后为其分配apprrpriate值,例如:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } //frame should be set accordingly(means should be framed accordingly). UILabel *l1=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100,100)]; UILabel *l2=[[UILabel alloc]initWithFrame:CGRectMake(110,10,100,80)]; UILabel *l3=[[UILabel alloc]initWithFrame:CGRectMake(115,60,100,50)]; [cell.contentView addSubview:l1]; [cell.contentView addSubview:l2]; [cell.contentView addSubview:l3]; return cell; }