2012-04-12 36 views
2

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

任何人都可以帮助我吗?

+0

你应该创建自定义tableviewcell,如果你需要它看起来像多个列 – Buron 2012-04-12 07:08:25

+0

@Buron你会请教关于我的问题的任何教程吗? – kumar 2012-04-12 07:11:22

回答

0
-(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并不是真正为m设计的多个列。但是你可以通过创建一个自定义的UITableCell类来模拟列。在Interface Builder中构建自定义单元格,为每列添加元素。给每个元素一个标签,以便您可以在控制器中引用它。

给您的控制器的插座从笔尖加载细胞:

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

然后,在你表视图委托的的cellForRowAtIndexPath方法中,通过标签分配这些值。

2

这是我做的:

#import <Foundation/Foundation.h> 

    @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]; 
相关问题