如何创建包含多行和多列的tableview?

我知道如何创建一个包含单列和多行的tableview,但我不知道如何创建包含多行和多列的tableview。

有谁能够帮助我?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { // load cell from nib to controller's IBOutlet [[NSBundle mainBundle] loadNibNamed:@"MyTableCellView" owner:self options:nil]; // assign IBOutlet to cell cell = myCell; self.myCell = nil; } id modelObject = [myModel objectAtIndex:[indexPath.row]]; UILabel *label; label = (UILabel *)[cell viewWithTag:1]; label.text = [modelObject firstField]; label = (UILabel *)[cell viewWithTag:2]; label.text = [modelObject secondField]; label = (UILabel *)[cell viewWithTag:3]; label.text = [modelObject thirdField]; return cell; } 

我认为这段代码可以帮助你UITableView并不是真正为多列设计的。 但您可以通过创建自定义UITableCell类来模拟列。 在Interface Builder中构建自定义单元格,为每列添加元素。 为每个元素添加一个标记,以便您可以在控制器中引用它。

给你的控制器一个sockets,从你的笔尖加载单元格:

 @property(nonatomic,retain)IBOutlet UITableViewCell *myCell; 

然后,在表视图委托的cellForRowAtIndexPath方法中,按标记分配这些值。

这就是我做到的方式:

 #import  @interface MyTableCell : UITableViewCell { NSMutableArray *columns; } - (void)addColumn:(CGFloat)position; @end 

执行:

 #import "MyTableCell.h" #define LINE_WIDTH 0.25 @implementation MyTableCell - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } - (void)addColumn:(CGFloat)position { [columns addObject:[NSNumber numberWithFloat:position]]; } - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); // Use the same color and width as the default cell separator for now CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0); CGContextSetLineWidth(ctx, LINE_WIDTH); for (int i = 0; i < [columns count]; i++) { CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue]; CGContextMoveToPoint(ctx, f, 0); CGContextAddLineToPoint(ctx, f, self.bounds.size.height); } CGContextStrokePath(ctx); [super drawRect:rect]; } @end 

和最后一块,cellForRowAtIndexPath

 MyTableCell *cell = (MyTableCell *)[rankingTableView dequeueReusableCellWithIdentifier:MyIdentifier]; cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];